31

In my info.plist, I added a key "Fonts provided by application" and added EdwardianScriptITCStd.otf enter image description here

But, when I try to get that font, it returns nil:

NSLog(@"%@", [UIFont fontWithName:@"EdwardianScriptITCStd" 
                                size:16.0f]);

I printed the font families using [UIFont familyNames] and it's not there either:

(
    Thonburi,
    "Snell Roundhand",
    "Academy Engraved LET",
    "Marker Felt",
    "Geeza Pro",
    "Arial Rounded MT Bold",
    "Trebuchet MS",
    Arial,
    Marion,
    "Gurmukhi MN",
    "Malayalam Sangam MN",
    "Bradley Hand",
    "Kannada Sangam MN",
    "Bodoni 72 Oldstyle",
    Cochin,
    "Sinhala Sangam MN",
    "Hiragino Kaku Gothic ProN",
    Papyrus,
    Verdana,
    "Zapf Dingbats",
    Courier,
    "Hoefler Text",
    "Euphemia UCAS",
    Helvetica,
    "Hiragino Mincho ProN",
    "Bodoni Ornaments",
    "Apple Color Emoji",
    Optima,
    "Gujarati Sangam MN",
    "Devanagari Sangam MN",
    "Times New Roman",
    Kailasa,
    "Telugu Sangam MN",
    "Heiti SC",
    "Apple SD Gothic Neo",
    Futura,
    "Bodoni 72",
    Baskerville,
    "Chalkboard SE",
    "Heiti TC",
    Copperplate,
    "Party LET",
    "American Typewriter",
    "Bangla Sangam MN",
    Noteworthy,
    Zapfino,
    "Tamil Sangam MN",
    "DB LCD Temp",
    "Arial Hebrew",
    Chalkduster,
    Georgia,
    "Helvetica Neue",
    "Gill Sans",
    Palatino,
    "Courier New",
    "Oriya Sangam MN",
    Didot,
    "Bodoni 72 Smallcaps"
)

What am I missing? Thanks!

0xSina
  • 20,973
  • 34
  • 136
  • 253
  • 4
    Have you verified that the font is included in your build target (in the "Copy Bundle Resources" build phase)? – inspector-g Apr 21 '12 at 00:43
  • 1
    Also, I would check the raw value of the key for "Fonts provided by application" to make sure it's correct. When viewing the *.plist in Xcode, right-click and choose "Show Raw Keys/Values", then verify the key is `UIAppFonts`. – inspector-g Apr 21 '12 at 00:46
  • Yes, the raw value is UIAppFonts and it's included in my copy bundle resources – 0xSina Apr 21 '12 at 00:52
  • An entire tutorial for custom fonts here - http://www.codigator.com/tutorials/using-custom-fonts-in-ios-application/ . Hope you will find your problem . Probably a naming problem or the font is not added to targets. – Bharat Gulati May 04 '13 at 19:57

6 Answers6

83

My comments were starting to get lengthy, so I've decided to submit an answer instead :)

  1. Custom fonts are only supported in iOS 3.2 and above, so make sure your Deployment Target build setting doesn't go back any farther than version 3.2.

  2. Verify that the font is included in your build target (in the "Copy Bundle Resources" build phase)

  3. Check the raw value of the key for "Fonts provided by application" to make sure it's correct. When viewing the *.plist in Xcode, right-click and choose "Show Raw Keys/Values", then verify the key is UIAppFonts.

  4. I believe iOS requires the PostScript name for a font when using fontWithName: size:, which you can find/verify by opening the font in Apple's Font Book and typing command+I.

  5. At one point, there was an issue with using custom fonts and UILabel. I am not sure if this problem still exists. The solution was to subclass UILabel and set the font therein.

If it still doesn't work, this Stack Overflow thread on custom fonts has quite a bit of info, you may have some luck there.

EDIT
There's no guarantee that I have the same font file as you, but on my machine the PostScript name is "EdwardianScriptITC"

Community
  • 1
  • 1
inspector-g
  • 4,146
  • 2
  • 24
  • 33
  • 1
    Thanks for the detailed response. #2 was the problem, even though I double checked, it was in fact not in my copy build phase (a sign that i need to go to bed soon). – 0xSina Apr 21 '12 at 01:34
  • 5
    FYI, "Fonts provided by", you have to add manually to your app's plist. The value in the array is the *filename*. The value passed to `[UIFont fontWithName:]` is the PostScript/font name. – Hari Honor Feb 20 '13 at 08:29
  • great list! #4 was my issue – aqs Jul 18 '13 at 12:26
  • #3 was what I was missing too – SeanR Mar 30 '15 at 05:53
  • useful info, +1 also #4 the issue – Bhavin_m Apr 12 '17 at 06:06
  • Another victim of #4 here, so +1!!! Just FYI. The Finder & Show Info tip is good, but... I had a case where even that didn't help (the full name was still "wrong"). I finally figured out the right string to use by using Interface Builder/Storyboard. Change the font on a label to a custom font and look for the name of the font in the list (provided you have it in the Info.plist and copy resources correctly). – atlwx Jul 08 '17 at 17:45
7

If your font name include '-' character, replace it with space character. For instance, Arial-Bold.ttf is used like [UIFont fontWithName:"Arial Bold" size:10];

SerkanHocam
  • 536
  • 6
  • 18
1

The font name to be used seems to be the "Family name" in the Font Book. I tried the PostScript name, but it does not always work. Family Name, as far as I can tell, has always worked up until now.

You can check this information, as mentioned in the above answer, inside Font Book, select the font and press cmd+I.

Hope this helps...

Sagi Mann
  • 2,967
  • 6
  • 39
  • 72
1

i was having the same issue. you can set font to a label from interface builder at storyboard. then get that font in debug mode from label like :

let cfnt = txtUsernameField.font

then click the i button on the little debug window and the family name of font will be writed on the console window. and you can use it like that :

txtUsernameField.font = UIFont(name: "MyriadPro-CondIt", size: 14) 
user3812138
  • 261
  • 4
  • 11
0

I had this problem although I swore I had custom fonts working just fine previously.

Turns out I just needed to run "Clean" on my project in XCode project so it could re-establish the references.

rmigneco
  • 595
  • 4
  • 16
0

The problem I ran into is that some of the fonts from the font family returned correctly while others returned nil.

For example:

  • MuseoSansRounded-500 valid
  • MuseoSansRounded-900 nil

It turned out that Xcode was automatically including custom fonts that had been used in Interface Builder. I had used the 500 weight font in IB but not the 900 weight one.

These two steps fixed it for me:

  1. In Build Settings, find and disable Auto-Activate Custom Fonts. There were two instances of this in my project.

  2. On app launch, manually register the custom fonts with the system. I used the snippet from this SO answer: https://stackoverflow.com/a/32600784/131378

In the name of completeness, I could have left Auto-Activate Custom Fonts turned on and then skipped registering those fonts in code. However, I didn't want to update the code during development if I ended up using a new font in IB that was previously being registered manually. Xcode complains if you register a font that was already auto-activated.

Mark Suman
  • 10,430
  • 2
  • 27
  • 23