Unable to set custom font for the UILabel in XCode

2020-06-04 12:19发布

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

This is what I've tried:

  • Download "JennaSue" font -- http://www.dafont.com/jenna-sue.font

  • Open "app-info.plist" under "Supporting Files" folder

  • Add new row in the list with key "Fonts provided by application"

  • In this array add "JennaSue.ttf"

  • Use it in the code like this:

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?

9条回答
女痞
2楼-- · 2020-06-04 12:54

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.

查看更多
Rolldiameter
3楼-- · 2020-06-04 12:56

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

查看更多
时光不老,我们不散
4楼-- · 2020-06-04 13:01

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

查看更多
放我归山
5楼-- · 2020-06-04 13:03

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

查看更多
ゆ 、 Hurt°
6楼-- · 2020-06-04 13:03

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.

查看更多
三岁会撩人
7楼-- · 2020-06-04 13:05
  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.

查看更多
登录 后发表回答