Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Fonts Xcode 4.3

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.

like image 392
Lolloz89 Avatar asked Feb 24 '12 14:02

Lolloz89


People also ask

How do I add custom fonts to Xcode?

To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.

Does TTF work on iOS?

You can install nearly any TrueType (. ttf) or OpenType (. otf) font on your iPad or iPhone. You can't change the system font, but you can use your installed fonts in Word, Excel, PowerPoint, Pages, Numbers, Keynote, Autodesk Sketchbook, Adobe Comp CC, and more.


2 Answers

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%.

like image 93
Shmidt Avatar answered Sep 30 '22 14:09

Shmidt


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

like image 28
Arne Avatar answered Sep 30 '22 12:09

Arne