Created
May 31, 2018 16:45
-
-
Save scott-lydon/0d889603d88bfc65562ac413e39196fe to your computer and use it in GitHub Desktop.
Get fonts from CocoaPods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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