2

Edit: I figured it out! Answer is posted below. Thanks JSD!

I must be missing something because I thought this was a fairly simple process. I have already copied the font over to my project folder. I am trying to use the font in a UILabel on a Table Cell with this code:

UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
cellTitle.font = [UIFont fontWithName:@"Source Sans Pro Black" size:50];
[cell.contentView addSubview:cellTitle];

It doesn't seem to be working. The text appears but not with the custom font. What am I missing?

EDIT: Apparently if I use this code instead

cellTitle.font = [UIFont fontWithName:@"Source Sans Pro" size:50];

This works but it's only using the last font in my array of my pList file, which is only the Regular version of the font. Has anyone ever had this issue before?

None of these codes work either:

cellTitle.font = [UIFont fontWithName:@"Source Sans Pro Bold" size:50];
cellTitle.font = [UIFont fontWithName:@"Source Sans Pro Regular" size:50];

This is the font in my font book:

enter image description here

PList file:

enter image description here

denvdancsk
  • 3,033
  • 2
  • 26
  • 41
  • It doesn't make a difference, and you should always include all your material assumptions in a body of your questions. – bioffe Jul 30 '13 at 17:07
  • I may be wrong, but don't fonts have to be .ttf – JeffRegan Jul 30 '13 at 17:17
  • It seems that the only way to get it to work is to use the family name with out 'Black' in the font name. The font that appears is whatever font is the last entry in my plist array. Weird. It's as if my app won't use more than one font or something. – denvdancsk Jul 30 '13 at 17:24
  • 1
    You can definitely have two custom fonts in one app. Can you try ttf fonts and see if that helps? I can't find anywhere that uses otf files. http://stackoverflow.com/questions/1371407/has-anyone-had-success-using-custom-otf-fonts-on-the-iphone – JeffRegan Jul 30 '13 at 17:42
  • 1
    Are you saying `[UIFont fontWithName:"SourceSansPro-Black" size:12]` and `[UIFont fontWithName:@"SourceSansPro-Regular" size:12]` end up looking the same on screen? Note that the name has to be exactly the way I showed it, with no spaces, and a hyphen. – jsd Jul 30 '13 at 17:53
  • Just tried that and it worked! I was using the family name that was displayed in fontbook but apparently Xcode renames them differently for their individual names because their all in the same family. – denvdancsk Jul 30 '13 at 18:00
  • 2
    You have to use the "postscript name" of the typeface you want. You can see it using "Get Info" on the font in Font Book. – nielsbot Jul 30 '13 at 18:15

4 Answers4

2

Make sure you add your font name to the info.plist file

lionserdar
  • 2,022
  • 1
  • 18
  • 21
  • Yep, got that too. The font is part of a family but I am only using the Black style for my app. – denvdancsk Jul 30 '13 at 17:02
  • 1
    if you copied the file to your project and then added the font name correctly to the plist it should work.That's how I do it and it works for me. Make sure the font name in the plist is exactly matching with your font file name. – lionserdar Jul 30 '13 at 17:07
  • It's weird, I have to remove 'Black' from the name but I only get the last font in my plist array. If I rearrange my array it only uses the last font. I can't call the font by their individual names. – denvdancsk Jul 30 '13 at 17:26
1

Add the font to your project. Then, in your info.plist file, add a section named Fonts provided by application as an array type, and each font is listed in its own row... Item0, Item1, etc.

When you load the font in code, you would load it as usual: [myUILabel setFont:[UIFont fontWithName:@"MyFontname" size:14]];. Usually you're going to be using the postscript name, which can be discovered using the FontBook application on your Mac.

Cameron
  • 1,142
  • 8
  • 32
1

Based on my usage of custom fonts your plist font name should match how the font is displayed in Font Book which should also match the fontWithName method. In your case I would expect to see Source Sans Pro Black in both places, specifically Source Sans Pro Black.otf in your plist and cellTitle.font = [UIFont fontWithName:@"Source Sans Pro Black" size:50]; in your code.

Dan
  • 5,153
  • 4
  • 31
  • 42
  • Yeah, it's crazy I can't call any of the fonts by their individual names only the font family name. The problem with that is ends up displaying whatever font is the last entry in my array in my plist. I am stumped. I am guessing it must be an issue with this particular font or something. – denvdancsk Jul 30 '13 at 17:34
  • 1
    Is your latest issue following the font or following the placing in the plist? By that I mean if you place a different font last on your plist does IT work instead of the Regular version? – Dan Jul 30 '13 at 17:35
  • Yep. If I move the bold font to the last of the list. That's what I get. The only way to get the Black font to work is to move it to the last of the list. That's not going to work because I eventually need all three. I am wondering if I have to export the fonts into another format or something. – denvdancsk Jul 30 '13 at 17:38
  • 1
    I was also going to suggest making sure you had the font in your bundle resources but it looks like you got it. FWIW the name you need to use in all of this is SourceSansPro- – Dan Jul 30 '13 at 18:22
1

I just got it working!!! Thanks to JSD for steering me in the right direction. Apparently the issue is how XCode renames font variants that are in the same family.

Using the Font Book name doesn't work!!!

You actually have to see how Xcode names the fonts. In order to do that you have to run a log command:

NSLog(@"%@", [UIFont fontNamesForFamilyName:@"Source Sans Pro"]);

gave me this:

enter image description here

This code worked:

cellTitle.font = [UIFont fontWithName:@"SourceSansPro-Black" size:50];
denvdancsk
  • 3,033
  • 2
  • 26
  • 41