Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: Using custom fonts inside Dynamic framework

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.

like image 853
Vojtech Vrbka Avatar asked May 28 '15 13:05

Vojtech Vrbka


People also ask

How do I add custom fonts to Xcode?

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.

How do I use custom fonts in SwiftUI?

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.

Can I use custom fonts with dynamic type in iOS 11?

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.

How do I scale a custom font for dynamic text style?

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.

What is a framework in Xcode?

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.

How do I use dynamic fonts in uifont?

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.


Video Answer


1 Answers

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")! ) 
like image 96
John Bushnell Avatar answered Oct 21 '22 13:10

John Bushnell