I added custom font to a framework. I followed all the steps, but it doesn't work.
I am able to set the font in Interface Builder, but when I build the project it doesn't show this font on the simulator/device.
To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.
To use a custom font, add the font file that contains your licensed font to your app, and then apply the font to a text view or set it as a default font within a container view. SwiftUI's adaptive text display scales the font automtically using Dynamic Type.
Using a custom font with dynamic type has always been possible but it took some effort to get it to scale for each text style as the user changed the dynamic type size. Apple introduced a new font metrics class in iOS 11 that makes it much less painful.
To make it easier to scale a custom font for dynamic type Apple introduced UIFontMetrics in iOS 11. To use a custom font for a given text style you first get the font metrics for that style and then use it to scale your custom font. Let’s revisit the example of setting a label to the .body text style but with a custom font.
Xcode utilizes a ‘framework’ concept to package up a well-defined collection of code and resources as a single library file for inclusion in a main project. Each framework should represent a separated feature that can individually be tested, built, shared, and reused.
To support dynamic type you set labels, text fields or text views to a font returned by the UIFont class method preferredFont (forTextStyle:). The returned font, which uses the Apple San Francisco typeface, has a size and weight adjusted for the users size preference and the intended text style.
I'm here a bit late, but I took PetahChristian's solution and created a Swift version in the form of an extension. This is working for me. I've found that when you try to get a font using a font name and a size using the regular way that it always looks in the main bundle for the font file, and there's no method that takes a bundle identifier as a parameter. It would be nice if Apple would make one.
Swift:
public extension UIFont { public static func jbs_registerFont(withFilenameString filenameString: String, bundle: Bundle) { guard let pathForResourceString = bundle.path(forResource: filenameString, ofType: nil) else { print("UIFont+: Failed to register font - path for resource not found.") return } guard let fontData = NSData(contentsOfFile: pathForResourceString) else { print("UIFont+: Failed to register font - font data could not be loaded.") return } guard let dataProvider = CGDataProvider(data: fontData) else { print("UIFont+: Failed to register font - data provider could not be loaded.") return } guard let font = CGFont(dataProvider) else { print("UIFont+: Failed to register font - font could not be loaded.") return } var errorRef: Unmanaged<CFError>? = nil if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == false) { print("UIFont+: Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.") } } }
Usage Example:
UIFont.jbs_registerFont( withFilenameString: "Boogaloo-Regular.ttf", bundle: Bundle(identifier: "com.JBS.JBSFramework")! )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With