Question

Following the tutorial at here. I am adding a custom font by copying the font to the project and also adding it to the file Info.plist. I also double check and see that the font is also added in "Copy bundle resource" in 'Build Phase'.

However, when I am trying to print the name of the font by following

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.myLabel.text       =   @"This is a custom font";
    self.myLabel.font       =   [UIFont fontWithName:@"Montserrat-Bold" size:36];
    NSLog(@"my font is %@",[UIFont fontNamesForFamilyName:@"Montserrat-Bold"]);
}

and I am getting

2013-02-15 22:20:36.014 UsingCustomFont[5064:11303] my font is (
)

Please look at the image for what I have done. enter image description here

Please help if you know what I am missing in the middle. Thanks

Was it helpful?

Solution

A couple of issues I've run into in the past:

  1. The font file may contain data in a format iOS does not support. In my experience, bitmap fonts don't work at all; only vector fonts are loaded.

  2. You may be using the wrong family name. Check the array returned by [UIFont familyNames] to make sure.

OTHER TIPS

First of all print all the fonts by below way:

// get font family
NSArray *fontFamilyNames = [UIFont familyNames];

for (NSString *familyName in fontFamilyNames)
{ 
   // font names under family
   for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName])
   {
       NSLog(@"Font Name = %@", fontName);
   }
}

Now, take the exact name of font which is listed by above code. Sometimes the names differed by iOS.

While assigning the font, you should use font name and not the name of the family.

Another common gotcha is that the font file was added to the project but not to the target. Select the font file in the organizer and check it's target membership in the "Identity and Type" inspector on the right. Make sure that your target is checked.

Try looking at all of the available fonts:

 for(NSString* family in [UIFont familyNames])
 {
    NSLog(@"%@", family);
   for(NSString* name in [UIFont fontNamesForFamilyName: family]) 
   {
     NSLog(@"  %@", name);
    }
 }

try this

  self.myLabel.text       =   @"This is a custom font";
  self.myLabel.font       =   [UIFont fontWithName:@"Montserrat-Bold" size:36];
  NSLog(@"my font is %@", myLabel.font.fontName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top