xcode custom ttf font not working

bkbeachlabs picture bkbeachlabs · Apr 28, 2012 · Viewed 21.1k times · Source

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

Answer

omz picture omz · Apr 28, 2012

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.