xcode custom ttf font not working

21,432

Solution 1

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.

Solution 2

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!

Share:
21,432
bkbeachlabs
Author by

bkbeachlabs

Sole Proprietor of BKBeachLabs in Toronto, Canada. Co-Owner of WebTO. Graduate of WesternU 2012, Honours B.Sc in Astrophysics & Music. Graduate of OISE-UToronto 2013, B.Ed. in Intermediate-Senior Math/Physics

Updated on January 23, 2020

Comments

  • bkbeachlabs
    bkbeachlabs over 4 years

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