I\'m creating an ios framework with its bundle for packaging ressources (nib, images, fonts) and I\'m trying to embed a custom font
Updated for Swift 4/5 and changed to throw errors instead of returning a Bool.
enum FontLoadingError: Error {
case fileNotFound
case unreadableFontData
}
func loadCustomFont(name: String) throws {
guard let fontURL = frameworkBundle.url(forResource: name, withExtension: "ttf") else {
throw FontLoadingError.fileNotFound
}
guard
let provider = CGDataProvider(url: fontURL as CFURL),
let font = CGFont(provider)
else {
throw FontLoadingError.unreadableFontData
}
var cfError: Unmanaged?
CTFontManagerRegisterGraphicsFont(font, &cfError)
if let error = cfError as? Error {
throw error
}
}