Custom Font in ios not working

Viewed 47579

I want to use HelveticaNeue-UltraLight in my ios application. I'm adding the font file as a resource to my project and adding the "Fonts provided by application" key in the plist file. The file name is HelveticaNeue.dfont and I've added it to the key array.

When I check for the available fonts I can see it now...

  NSArray *fonts = [UIFont fontNamesForFamilyName:@"Helvetica Neue"];

for(NSString *string in fonts){
    NSLog(@"%@", string);
}

1-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-Bold
2011-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-CondensedBlack
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue-Medium
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-Light
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-CondensedBold
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-LightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLight // HERE IT IS!
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-BoldItalic
2011-07-08 17:32:57.870 myApp[5159:207] HelveticaNeue-Italic

But when I try to get it with [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:12] I just get HelveticaNeue-Light..

I'm getting no errors or warnings. Help please!

17 Answers
  1. Drag your font files (.ttf or .otf) into project's bundle.
  2. Then select all those fonts > on your xcode's right panel make sure the Target membership is enabled.
  3. Select project from the navigator panel > select Build phases > under Copy Bundle Resources verify all your fonts are listed. If not, add one by one using + icon.
  4. Info.plist > select Fonts provided by application > add your fonts one by one (e.g., Helvetica-Bold.ttf).

In addition to:

  • adding the font file names to the info.plist under "Fonts provided by application"
  • adding the actual files to the project.

You also have to make sure that under Targets -> Build Phases -> Copy Bundle Resources has the actual filenames of the custom fonts.

To check if they have appeared, you can do something like:

        UIFont.familyNames.forEach { (name) in
        print(name)
    }

Another thing to check is on a Mac: Open the app Font Book. Then go file -> file validation and open the font. If something is failing (even with a warning) in there iOS generally rejects loading the font in the app with no errors.

If this is the case then you can fix the errors by opening the font in a free app called "Typelight" And resaving the font "Save as".

In addition to the well explained @Ben answer, if you're still not getting your fonts, Just use or assign your font to any Label in one of your Storyboard, You will start getting it using this code

If you still unable to get font using [UIFont fontWithName:@"Font-Regular" size:11]; There must be problem in naming. Stop at any Breakpoint in debugger and Debug it using different names, and see which po prints object that is not nil

po [UIFont fontWithName:@"FontRegular" size:11];

or

po [UIFont fontWithName:@"Font Regular" size:11];

or

po [UIFont fontWithName:@"Font-Regular" size:11];

You will get <UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt for actual font name otherwise you will get nil

note that add .ttf end of each Fonts provided by application name

It's an old question but I'd like to emphasize that we must write font file name WITH EXTENSION into info.plist. It was particularly my mistake: I missed extensions. But the very interesting thing that iOS successfully loaded three fonts even when the extensions were missed. Once I've added extensions all 12 fonts were loaded.

Good article: https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

Related