31

ok so I've been reading through SO and doing all sorts of googling but I cant figure out why my font isn't working.

I think I've done everything right, but when I run the app, the text on my button appears with the standard system font instead of the one I imported. I added NSLog(@"%@",[UIFont familyNames]); to see if it was in the list but it isnt. Which makes me think I've set it up wrong.

Im hoping someone can help me display the text in my label in my custom font. Thanks to anyone who thinks they might have any suggestions!

Step-by-step this is what I've done.

Step 1: I downloaded .ttf file from the internet. In my finder it looks like: enter image description here

Step 2: I dragged the font file into XCode from finder and check the "copy file to project folder" option. So in my project I can see:

enter image description here

Step 3: I opened the font in font book to see what the real filename is and I see this: enter image description here

Step 4: I added a key to my MyApp-Info.plist file with the filename from XCode including the file type. It looks like this:

enter image description here

Step 5: And then in my code I write this:

UIButton *thisLevelButton = [UIButton buttonWithType:UIButtonTypeCustom];

[thisLevelButton setBackgroundImage:[UIImage imageNamed:@"ccLSButtonPlayed"] forState:UIControlStateNormal];
thisLevelButton.frame = CGRectMake(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
thisLevelButton.tag = j;
[thisLevelButton addTarget:self action:@selector(userSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
[thisLevelButton.titleLabel setFont:[UIFont fontWithName:@"PressStartK" size:24]];
[thisLevelButton setTitle:[NSString stringWithFormat:@"%i",(j+1)] forState:UIControlStateNormal];

// add the button to the panel
[subScroll addSubview:thisLevelButton];

For reference, this is what's printed in the list of font families:

(
    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"
)
bkbeachlabs
  • 2,121
  • 1
  • 22
  • 33
  • Thank you for your question!! I followed the instructions assuming it's an answer to a related question, and YES! My problem is fixed now, and that's not for English fonts, but Persian ones. I've been pulling out my hair for over two years and could never find a proper solution even with custom APIs, etc. But apparently Apple is being keen this time, letting us use a custom font just with adding that key to the info.plist. Thanks a again! – Neeku Nov 21 '13 at 10:57

2 Answers2

38

I would suspect that you haven't added the font file to your target, so that it's not copied to the app's resources. Your code works fine here and the font does show up as "Press Start K" in the list of font families.

omz
  • 53,243
  • 5
  • 129
  • 141
  • thankyou!! This was so annoying. I guess when i dragged the file in I didnt actually copy to the target. So i deleted the ttf file, then manually went to Add Files to "MyApp". That worked – bkbeachlabs Apr 28 '12 at 21:07
  • 13
    Xcode's file inspector (`⌘⌥1`) also has a "Target Membership" section where you could simply activate the checkbox of your target, instead of removing and re-adding the file. – omz Apr 28 '12 at 21:15
12

Another possibility is that you might be calling the wrong font name when you try to call your font. Use:

Swift 2.1+

//Check which fonts available
for family: String in UIFont.familyNames()
{
   print("\(family)")
   for names: String in UIFont.fontNamesForFamilyName(family)
   {
       print("== \(names)")
   }
}

Objective C

//Check which fonts available
for (NSString* family in [UIFont familyNames])
{
    NSLog(@"FONT %@", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family])
    {
        NSLog(@"  %@", name);
    }
}

To find the actual names of the fonts you should be using with UIFont(name: "yourFontName", size: 24.0) (swift) or [UIFont fontWithName:@"yourFontName" size:16.0f] (obj c). The custom font names you add to your plist must match whatever the files are called in your directory (including the .ttf or .otf), but the code referencing the fonts must match the font names you get from this output. Good luck fontmasters!

Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
  • 1
    thanks, I was having a similar issue and it turns out the system was referencing the font as a different name then what I thought it was. – sudo Apr 27 '14 at 16:39
  • I suggest everyone to do that before using the font name!Great suggestion! – arniotaki May 22 '14 at 08:47