5

I'm trying to use this font in my project but it won't work. I added the .ttf file in my project and added its name in the MyApp-Info.plist under the key: Fonts provided by application. Then I tried this:

 [_titleLabel setFont:[UIFont fontWithName:@"chalkboard" size:_titleLabel.font.pointSize]];

I also checked if the name of the font is really "chalkboard" and it is. The displayed font is still Helvetica, only smaller. Where could the problem be? Thanks in advance.

Dejell
  • 13,947
  • 40
  • 146
  • 229
Lolloz89
  • 2,809
  • 2
  • 26
  • 41

6 Answers6

32

You need to use font name, not filename. Font name is inside the ttf file.

You can find font names for iOS here: http://iosfonts.com/

This code will list all your font names in your app, put it somewhere in viewDidLoad on main controller, run app and then in console found the right name for the font.

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];

NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
}

Upd.

You can also right click on font file in Finder, get Info and copy full name. But it is not always 100%.

Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • As I said, I actually used the font name, the one I read when I opened the font and it didn't work. Unfortunately (try opening the link) the chalkboard font I would use is not the same one installed on iOs.. – Lolloz89 Feb 24 '12 at 20:42
  • Have you tried [_titleLabel setFont:[UIFont fontWithName:@"Chalkboard-Regular" size:_titleLabel.font.pointSize]]; – Shmidt Feb 25 '12 at 07:13
  • Try to find out family name with this code(I updated my answer) – Shmidt Feb 25 '12 at 11:23
  • 2
    @Flink: Have you tried your font-listing code in Xcode 4.3 with a custom font installed? Because it hasn't worked for me. – Tim Sullivan Feb 25 '12 at 22:18
  • Yes, I tried and it works. OK, it seems that when you added the font to the project you didn't check "add to target", so font isn't used now. Choose "target-build phases-copy bundle resources" and add your font. – Shmidt Feb 26 '12 at 09:06
  • Last thing — check that font filename is written with extension(.ttf) in app-info.plist. After that paste here console output. – Shmidt Feb 26 '12 at 13:13
  • 1
    Anyone find a answer to this issue? I am on 4.3.2 and can confirm that the custom fonts are coming up in the above font listing code, but are not showing up in the list in the identity browser. – Rob Bonner Mar 27 '12 at 12:13
  • Just wanted to add for future reference: I couldn't find the problem why my font, with the fontname in OS X fontbook being "Function-Caps-Regular", would not load in my iOS App until I used this script, where it was named "FunctionCapsRegular", without the "-". Once changed, the font could be loaded.. I can't understand this inconsistency.. – hreimer Apr 02 '13 at 11:50
  • Works for me on Xcode 4.6. As noted int the post just above this by hajn, be careful when you call the font. The font file name is not the same as the font name. That's why this method is so useful. For example, I added "Bradley Hand ITC TT-Bold" font file to my project as described in the OP, but the font name is actually, BradleyHandITCTT-Bold. – JScarry Jun 11 '13 at 21:35
24

Flinks answer is a handy tool to check if your fonts are loaded properly.

Do the following:

[_titleLabel setFont:[UIFont fontWithName:@"chalkboard" size:_titleLabel.font.pointSize]];

Make sure your fonts are included in your bundle:

  • Click your project
  • Go to build phases
  • Under 'Copy Bundle Resources', make sure your fonts are included

Worked for me :)

Arne
  • 424
  • 2
  • 5
3

Xcode 4.3.1 Custom Font have problem. Because when i create new project and try to add custom fonts in Xcode 4.2.1 there is no problem.

I did several times. Interesting...

Arda Dtmr
  • 125
  • 2
  • 10
1

Its my experience that not "all" fonts are compatible. Most work, but you will run into a few fonts that just for some reason will not be recognized through code no matter how hard you try... Just a heads up for anyone having issues, just try another font!

FreeAppl3
  • 858
  • 1
  • 15
  • 32
  • I thought I'd run into one when I tried to add Bitstream Vera Sans Mono. It turns out that the steps above work great for adding the font to the app. I displays fine in a UITableView but it doesn't seem to work as a font choice for drawInRect. – JScarry Jun 19 '13 at 01:08
1

  1. Include your fonts in your XCode project
  2. Make sure that they’re included in the Target Membership in File Inspector tab
  3. Include you font in Project Name > Build Phase > Copy Bundle Resources
  4. Include your iOS custom fonts in your application plist. Add a new row called “Fonts provided by application” and put your font name and extension as parameter.
  5. Find the name of the font.
  6. Use UIFont and specify the name of the font.

    UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 290, 300, 30)];
    headingLabel.text = @"WELCOME";
    headingLabel.font = [UIFont fontWithName:@"Arvo" size:14.0];

Source: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
Azam
  • 797
  • 2
  • 8
  • 23
0

Not All Font's you imported are working. Just try to test into another font. If it works then it is not recognized by Xcode. Just remember that it must be in your project plist file and it has to be a extension name like ".otf" or ".ttf" in plist. And also add it in "Copy Bundle Resources" under target