Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Fonts only available when set in Interface Builder

Tags:

ios

fonts

I have added a custom font to my project. It is included in the target, and it is added in the plist. When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts. However, it does show up as an option in Interface Builder, and if I set a label's text to that font in IB, it works correctly and shows up when I print out a list of available fonts. This is XCode 6.4 and iOS 8.0

When it is working via IB, it gets printed out in the font names like this: Special Elite SpecialElite-Regular

I call the font programmatically like: [UIFont fontWithName:@"SpecialElite-Regular" size:fontSize];

There are no problems when doing this with the built-in fonts.

like image 441
MaxB Avatar asked Jul 15 '15 23:07

MaxB


1 Answers

When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts

This proves that you have not in fact included it properly in the target and the Info.plist.

The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.

Your font is Special Elite. As you can see, I have it visible here in my running app:

enter image description here

Here's the code that I used:

    let lab = UILabel()
    lab.text = "This is a test"
    lab.font = UIFont(name:"SpecialElite-Regular", size:18)
    lab.sizeToFit()
    lab.frame.origin = CGPointMake(100,100)
    self.view.addSubview(lab)

So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.

(Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.)

like image 152
matt Avatar answered Sep 30 '22 13:09

matt