Custom Fonts Xcode 4.3
Asked Answered
C

6

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.

Contortive answered 24/2, 2012 at 14:29 Comment(1)
I've been having this problem since moving to Xcode 4.3 as well. No matter what I do, custom fonts don't install.Egotist
E
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%.

Estipulate answered 24/2, 2012 at 17:51 Comment(9)
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..Contortive
Have you tried [_titleLabel setFont:[UIFont fontWithName:@"Chalkboard-Regular" size:_titleLabel.font.pointSize]];Estipulate
Try to find out family name with this code(I updated my answer)Estipulate
@Flink: Have you tried your font-listing code in Xcode 4.3 with a custom font installed? Because it hasn't worked for me.Egotist
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.Estipulate
Last thing — check that font filename is written with extension(.ttf) in app-info.plist. After that paste here console output.Estipulate
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.Ironstone
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..Nette
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.Subserve
B
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 :)

Bartholomew answered 26/3, 2012 at 11:0 Comment(5)
Thanks for this answer ame. This was the "missing step" that I was looking for. ;-)Lowly
I've been searching for an answer to this for ages! It was the "Copy Bundle Resources" that was getting me! Thanks so much!Massie
I never needed the "Copy Bundle Resources" step until 4.3.2, so I'm glad to have found this after five hours of pulling my hair out.Saddlebow
thanks for the copy bundle resources. i owe you a huge pint of german beer ;)Souza
Will keep you to that, thanks ;) But seriously, glad I could help :)Bartholomew
C
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...

Chatelaine answered 20/3, 2012 at 8:14 Comment(0)
O
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!

Oculus answered 21/4, 2013 at 13:43 Comment(1)
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.Subserve
L
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/
Lavellelaven answered 13/9, 2013 at 8:2 Comment(0)
P
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

Ppi answered 18/3, 2014 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.