-
-
Save kerimdeveci/22c5bcdec4a3dee612d372ffe0375662 to your computer and use it in GitHub Desktop.
A UIFont extension in swift to load fonts from within a bundle
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 | |
| public extension UIFont { | |
| class func loadAllFonts(bundleIdentifierString: String) { | |
| registerFontWithFilenameString(filenameString: "icon-font.ttf", bundleIdentifierString: bundleIdentifierString) | |
| // Add more font files here as required | |
| } | |
| static func registerFontWithFilenameString(filenameString: String, bundleIdentifierString: String) { | |
| if let frameworkBundle = Bundle(identifier: bundleIdentifierString) { | |
| let pathForResourceString = frameworkBundle.path(forResource: filenameString, ofType: nil) | |
| let fontData = NSData(contentsOfFile: pathForResourceString!) | |
| let dataProvider = CGDataProvider(data: fontData!) | |
| let fontRef = CGFont(dataProvider!) | |
| var errorRef: Unmanaged<CFError>? = nil | |
| if (CTFontManagerRegisterGraphicsFont(fontRef, &errorRef) == false) { | |
| print("Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.") | |
| } | |
| } | |
| else { | |
| print("Failed to register font - bundle identifier invalid.") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment