Last active
May 24, 2017 05:29
-
-
Save iamjason/6a32aa4f118d49bb8932716b15394386 to your computer and use it in GitHub Desktop.
Loading Custom Fonts - Swift 3
This file contains hidden or 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
import UIKit | |
/// Load Fonts | |
/// | |
/// Example Usage: | |
/// | |
/// let fonts = ["SourceSansPro-Regular.otf", "GT-Walsheim-Regular.ttf"] | |
/// | |
/// let loadedSuccessfuly = loadFonts(fonts) | |
/// | |
/// - parameter fonts: Array of font names to load including extentions e.g. .otf or .ttf | |
/// | |
/// - returns: TRUE if ALL fonts loaded correctly | |
/// | |
public func loadFonts(_ fonts:[String]) -> Bool { | |
return fonts | |
.map { (str) -> (String, String) in | |
let components = str.components(separatedBy: ".") | |
return (components[0], components[1]) | |
} | |
.filter { (fileComponents) -> Bool in | |
if let fontURL = Bundle.main.url(forResource: fileComponents.0, withExtension: fileComponents.1) { | |
CTFontManagerRegisterFontsForURL(fontURL as CFURL, CTFontManagerScope.process, nil) | |
return true | |
} | |
return false | |
}.count == fonts.count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment