5

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.

MaxB
  • 215
  • 3
  • 10
  • `[UIFont fontWithName:@"SpecialElite-Regular" size:fontSize];` Okay, good. But is the result `nil`? – matt Jul 15 '15 at 23:52
  • yes. the font is nil – MaxB Jul 15 '15 at 23:56
  • So it's not being loaded under that name. – matt Jul 16 '15 at 00:00
  • but it is loaded under that name when I do it from IB because that's how it shows up when I print out all available fonts – MaxB Jul 16 '15 at 00:03
  • But you're not loading it properly. I insist! – matt Jul 16 '15 at 00:04
  • See my revised answer. If you load it correctly, you can refer to it in code. The fact that you think you can do it from IB is irrelevant. – matt Jul 16 '15 at 00:06
  • Haha, I believe you! Is there something else that could be going wrong with loading it other than the name? – MaxB Jul 16 '15 at 00:06
  • I don't know how you're loading it. I've already said several times, it needs to be in the target / copy build phase and it needs to be listed correctly in your Info.plist. – matt Jul 16 '15 at 00:08
  • Or maybe `fontSize` has a bad value! – matt Jul 16 '15 at 00:09

6 Answers6

2

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.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • That's interesting, but it does work when I run it on the device – MaxB Jul 15 '15 at 23:42
  • Then there must be something wrong with how you're referring to it in code. You are probably using the wrong name - learning the correct name is not easy! Show your code for referring to it, and your logging of the available fonts. – matt Jul 15 '15 at 23:43
  • Edit your question - you can't show this kind of stuff in a comment. :) – matt Jul 15 '15 at 23:48
2

Ok, topic is old but I would like to put in my two cents here.

First off all, read this article http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

In brief:

  1. remember to include your fonts to project
  2. setup target for fonts
  3. check if your fonts are included as resources in bundle
  4. add custom fonts to plist file
  5. check the real name of your font in code

When I did everything like described in this article, my fonts works in interface builder only. I also have problem with step 5 - my fonts did not show up in console output.

Ok, so now is time for tips from me:

TIP #1

use otf font format, if you have different, convert it (or ask your designer ;) ) you can use this site for that: https://onlinefontconverter.com/

TIP #2

In article above you can find:

Open it and add a new row called “Fonts provided by application” which will be an array that you need to add all the filenames of the fonts you want to use.

Instead of "Fonts provided by application" enter "UIAppFonts" Remember, if you have more than one plist file, set this for all.

1

As mentioned by others, following article is very useful to diagnose the problem. http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

Also while adding custom fonts to Xcode project, add individual font and not the directory in which all fonts are stored. If you add that directory, fonts will be visible in Interface Builder but not in the running app.

0

Matt did a great job of isolating the problem... I was not loading the font correctly.

In my case the problem was in the Info.plist.

I was listing the font in the Info.plist using its name. This is incorrect. You are supposed to list the font in the Info.plist using its file name.

MaxB
  • 215
  • 3
  • 10
0

I know this is old but I just ran into the same issue. The font would show up only if I selected that font in the interface builder. Turns out when I searched for UIAppFonts, it didn't list the main Info.plist (we were already using custom fonts) for some reason. So I ended up adding the fonts to the localized (?) plist file which resulted in this issue. What I had to do was to go to the Project Settings > Target > Info tab and add the font file names there.

newDeveloper
  • 1,365
  • 1
  • 17
  • 27
0

Issue is You are not giving ProperName Of Fonts in Code, for Getting Name of Fonts please run this code.

 for family: String in UIFont.familyNames
    {
        print("\(family)")
        for names: String in UIFont.fontNames(forFamilyName: family)
        {
            print("== \(names)")
        }
    }

it will prints all fonts, search your font and give proper name.

 let attributedText = NSMutableAttributedString(string: "We 
 Apologize! \n This feature is only available for \n Go 4.0 
 Subscriptions", attributes: [NSAttributedStringKey.font : 
 UIFont(name: "SpecialElite-Regular", size: 22)!])
AyAz
  • 2,027
  • 2
  • 21
  • 28