0

Possible Duplicate:
Custom font in iPhone

I'm trying to create a custom label to set it as a titleView of my navigationItem, but I'm not able to set the label font to a custom font included im my project.

I have a custom font file called "BADER_AL_GORDABIA-2_0.ttf" and the real font name as shown in FontBook is "bader_al gordabia-2" I have followed these steps:

  1. checked the deployment target of my app, and it is set to iOS 5.1
  2. added the font file to project resources, and verified that is is added to the build target in the "Copy Bundle Resources" in Build Phases.
  3. added a new key in my info.plist file as "Fonts provided by application", and added the "BADER_AL_GORDABIA-2_0.ttf" inside it.
  4. used the real font name "bader_al gordabia-2" in fontWithName method.

I was able to load the font using

UIFont * customFont = [UIFont fontWithName:@"bader_al gordabia-2" size:18.0f];

and I made sure that customFont is not null (by printing its description), but the problem is I can't apply this custom font to a UILabel.

my code is:

UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,10,200,24)];
    titleLabel.backgroundColor = [UIColor clearColor];
    UIFont * customFont = [UIFont fontWithName:@"bader_al gordabia-2" size:18.0f];
    [titleLabel setFont:customFont];
    titleLabel.text = @"أخبار الجمعية";
    //NSLog(@"%@", [customFont description]);

    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];

    [self.navigationItem setTitleView:titleLabel]; 

I have tried to search stack Overflow and I wasn't able to find a suitable answer. I have tried also to use a custom label as specified in the following link: http://refactr.com/blog/2012/09/ios-tips-custom-fonts/

Community
  • 1
  • 1
Roula
  • 99
  • 4

2 Answers2

1

This is how i am doing it,

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 100)];

navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = @"Set Up";
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
navLabel.shadowOffset = CGSizeMake(0, -2);
navLabel.font = [UIFont fontWithName:@"Questrial" size:28.0];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];

Only above code is not working ... So you need to go to your app plist file and add a new row and add this key to the row,

Fonts provided by application

and add the font name to the value of item0,

According to my code it is, "Questrial-Regular.ttf"

Also another thing,

That is you may not know the font name correctly. So you need to find the correct font name. Add this code to your ViewdidLoad and it will log the family names to the terminal.

NSLog(@"%@",[UIFont familyNames]);

And you can find correct font name according to your font.

Follow this steps, it worked for me and i think it will help you too.

Sameera Chathuranga
  • 3,638
  • 3
  • 27
  • 48
0

Here's what you should do - First add and call the following method anywhere:

+(void)logAllFonts{
    NSArray *fontFamilies = [UIFont familyNames];

    for (int i = 0; i < [fontFamilies count]; i++)
    {
      NSString *fontFamily = [fontFamilies objectAtIndex:i];
      NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
      NSLog(@"%@: %@", fontFamily, fontNames);
    }
 }

This will log all the fontNames present in your project including the external fonts that you have added both in the project and the plist.

Now you go through that list in the console and find the exact font name for the font you added (bader_al gordabia-2). Use this name in fontWithName:

Hope this helps

Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
  • sorry, I found the exact name of the font using loggAllFonts, but the label still refuses to apply the font to its text. – Roula Jan 30 '13 at 21:10