FrozenHeart
FrozenHeart

Reputation: 20746

Unable to set custom font for the UILabel in XCode

I am unable to set custom font for the UILabel in XCode.

This is what I've tried:

self.someLabel.font = [UIFont fontWithName:@"JennaSue" size:14];

And nothing happens -- the default font is visible.

Why? What am I doing wrong? How can I fix it?

Upvotes: 5

Views: 7977

Answers (9)

Boris Nikolic
Boris Nikolic

Reputation: 756

Here is @Ritu's answer in Swift 2. Just put this in your AppDelegate, or in viewDidLoad method of your View Controller:

let names = UIFont.familyNames()
for name in names {
   print("Font Family: \(name)")
   let fontFaces = UIFont.fontNamesForFamilyName(name)
   for fname in fontFaces {
      print("            \(fname)")
   }
}

You will have all your font names in console log, just copy and paste the one you need in your code.

Upvotes: 0

Geet
Geet

Reputation: 2437

is the file visible in Compile Source now…if yes then you may have the font name wrong..sometimes the font names are different then their file name…try running a

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

this lines of code..you will be able to see the actual name of the font and use that name instead

Upvotes: 0

Saleh Albuga
Saleh Albuga

Reputation: 448

The font could be corrupted or something. I tried the solutions provided by Gabriel.Massana & Neeku and it worked on some fonts. Try adding the font to your Mac. If it showed verification problem, most probably it's not gonna work in your app. Hope this helps

Upvotes: 0

ios_dev
ios_dev

Reputation: 113

After adding custom font to Xcode don't forget to add font to plist. Project settings > Info > Custom IOS target properties

enter image description here Add property Fonts provided by application and type your custom font(filename of font file). Then you can use that code example self.someLabel.font = [UIFont fontWithName:@"JennaSue" size:14];

Notice: make sure that you use right font name in code. To do that you should add this font to your Mac's font book, open font book, choose that font and check the right name font.

Hope this helps

Upvotes: 1

Gabriel.Massana
Gabriel.Massana

Reputation: 8225

Be sure your font is in Bundle Resources. For some reason Xcode it is not importing custom font properly most of the time:

enter image description here

enter image description here

enter image description here

I've got the font working:

enter image description here

Example code: here

Upvotes: 14

Neeku
Neeku

Reputation: 3653

  1. Go to your project's info.plist file,
  2. Right click and select Add row,
  3. Type Fonts provided by application in the new row,
  4. Type in the desired font name as items for this key.
  5. Drag and drop the font file into the project,
  6. Apply it to the text you want:

someLabel.font = [UIFont fontWithName:@"JennaSue" size: 12.0];

I think you've missed step 5 up there.

Update: When doing step 5, remember to check the marks for copying the actual file into project directory:

enter image description here Remember to clean your project by pressing "command+alt+shift+K" (IRRC!) And then go to your

and then go to your project's Build Phases, and make sure you can see your .ttf file file among the resource files:

enter image description here

P.S. I'm not on my Mac at the moment, so I used screenshots from this tutorial. That's why I grayed out the unnecessary lines for your issue.

Upvotes: 5

Ritu
Ritu

Reputation: 671

Check this code:

 NSArray *names = [UIFont familyNames];
 NSLog(@"Font FamilyNames : ");
 for (NSString *name in names) {
     NSLog(@"Font Family:  %@",name);
     NSArray *fontFaces = [UIFont fontNamesForFamilyName:name];
     for (NSString *fname in fontFaces) {
          NSLog(@"   %@",fname);
     }
  }

  self.someLabel.font = [UIFont fontWithName:@"use correct name" size:self.someLabel.font.pointSize];

and use the same name printed with NSLog.

Upvotes: 2

Jasmin
Jasmin

Reputation: 794

i used following code and it's done

 UILabel * lblTest = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 100)];

    lblTest.font = [UIFont fontWithName:@"JennaSue" size:25.0];
    lblTest.text = @"Hello World";

    [self.view addSubview:lblTest];

and if still not working looking at your system font book and search that font and That Font you have to use to get it work

also try this lblTest.font = [UIFont fontWithName:@"Jenna Sue" size:25.0];

Upvotes: 0

Hardik Kardani
Hardik Kardani

Reputation: 576

self.someLabel.font = [UIFont fontWithName:@"JennaSue.ttf" size:self.someLabel.font.pointSize];

Upvotes: 0

Related Questions