I added custom font to a framework. I followed all the steps, but it doesn\'t work.
I am able to set the font in Interface Builder, but when I build the project it d
Was able to do this with Swift 4, given that you can include resources directly in a framework bundle now:
Typography.swift (in my framework)
import Foundation
private class MyDummyClass {}
func loadFontWith(name: String) {
let frameworkBundle = Bundle(for: MyDummyClass.self)
let pathForResourceString = frameworkBundle.path(forResource: name, ofType: "otf")
let fontData = NSData(contentsOfFile: pathForResourceString!)
let dataProvider = CGDataProvider(data: fontData!)
let fontRef = CGFont(dataProvider!)
var errorRef: Unmanaged? = nil
if (CTFontManagerRegisterGraphicsFont(fontRef!, &errorRef) == false) {
NSLog("Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
}
}
public func loadMyFonts() {
loadFontWith(name: "ComicSansPro-Regular")
loadFontWith(name: "ComicSansPro-Medium")
loadFontWith(name: "ComicSansPro-Bold")
loadFontWith(name: "ComicSansPro-ExtraBold")
}
I ended up requiring the loadMyFonts method to be called in the application's that use this framework's didFinishLaunchingWithOptions method in AppDelegate.swift.