Skip to content

Instantly share code, notes, and snippets.

@scott-lydon
Created May 31, 2018 16:45
Show Gist options
  • Save scott-lydon/0d889603d88bfc65562ac413e39196fe to your computer and use it in GitHub Desktop.
Save scott-lydon/0d889603d88bfc65562ac413e39196fe to your computer and use it in GitHub Desktop.
Get fonts from CocoaPods
public final class Fonts {
static func podFont(name: String, size: CGFloat) -> UIFont {
//Why do extra work if its available.
if let font = UIFont(name: name, size: size) {return font}
let bundle = Bundle(for: Fonts.self) //get the current bundle
let url = bundle.url(forResource: name, withExtension: "ttf")! //get the bundle url
let data = NSData(contentsOf: url)! //get the font data
let provider = CGDataProvider(data: fontData)! //convert the data into a provider
let cgFont = CGFont(provider)! //convert provider to cgfont
let fontName = cgFont.postScriptName as! String //crashes if can't get name
CGFontManagerRegisterGraphicsFont(cgFont, nil) //Registers the font, like the plist
return UIFont(name: fontName, size: size)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment