Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom font showing up in IB but not on device

I'm trying to use fonts from the Open Sans family. I added the fonts to my Xcode project, I checked that they were added to my application's resources bundle and I added the fonts to my Info.plist file.

When I edit a XIB in Interface Builder, I can use Open Sans font on UILabels when selecting Custom in the font dropdown menu. The font are correctly rendered on the preview in Interface Builder, but then when I launch the application on the device, the font is not applied to the labels.

I tried setting the font programmatically, but that didn't work either. And I'm not getting any warning nor error.

What did I forgot to be able to use a custom font?

The behavior has been the same on an iPad Air running iOS7 and on an iPhone 6 running iOS8.

like image 426
JBL Avatar asked Mar 30 '15 15:03

JBL


3 Answers

Go to Target -> Build Phases -> Copy Bundle Resources , in there you check if the font file is there, if not press the plus button and add it manually. Sometimes the font files are not added automatically to the bundle resources.

Hope this helps.

like image 158
stan Avatar answered Oct 09 '22 08:10

stan


Just make sure all the fonts really are available in the bundle, try printing all fonts and check if you are using the correct name. You can very easily do this with following code in app delegate's didFinishLaunchingWithOptions method:

for (NSString *familyName in [UIFont familyNames]) {
    NSLog(@"Family Name : %@", familyName);
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
        NSLog(@"\tFont Name : %@", fontName);
    }
}

or in Swift:

if let familyNames = UIFont.familyNames() as? [String] {
    for familyName in familyNames {
        println("Family : " + familyName)
        if let fontNames = UIFont.fontNamesForFamilyName(familyName) as? [String] {
            for fontName in fontNames {
                println("\tFont : " + fontName)
            }
        }
    }
}

The swift code is not the most efficient, but should work for checking if the fonts exist in the bundle.

like image 3
Swapnil Luktuke Avatar answered Oct 09 '22 10:10

Swapnil Luktuke


What worked for me was when I added the font to check the "Add to targets" thick in front of my app name.

See image example here :

enter image description here

like image 2
Iancu Tudor Avatar answered Oct 09 '22 08:10

Iancu Tudor