Unable to set custom font for the UILabel in XCode

2020-06-04 12:26发布

问题:

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?

回答1:

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

I've got the font working:

Example code: here



回答2:

  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:

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:

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.



回答3:

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.



回答4:

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

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:

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


回答6:

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];



回答7:

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



回答8:

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



回答9:

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.