Adding custom fonts to iOS app finding their real names

52,188

Solution 1

Use +[UIFont familyNames] to list all of the font family names known to the system. For each family name, you can then use +[UIFont fontNamesForFamilyName:] to list all of the font names known to the system. Try printing those out to see what name the system expects. Example code:

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

Put that in your app, call it, and see what you get. If you see a name in the output that looks appropriate for your font, use it. Otherwise, perhaps you haven't properly added the font to your app.

In Swift:

func dumpAllFonts() {
    for familyName in UIFont.familyNames {
        for fontName in UIFont.fontNames(forFamilyName: familyName) {
            print(fontName)
        }
    }
}

Solution 2

This is Step for, How to add custom font in Application.

1 - Add .TTF font in your application
2 - Modify the application-info.plist file.
3 - Add the key "Fonts provided by application" to a new row
4 - and add each .TTF file (of font) to each line.

Also read This and This tutorials for improve your knowledge.

FOR MOREINFORMATION :

But after adding font to your application, sometimes might be the font name is not the name of the file name so you need to apply/write real name of your font, so check @NSGod's answer.

Following is code for find all fonts from the system:

for(NSString *fontfamilyname in [UIFont familyNames])
{
    NSLog(@"Family:'%@'",fontfamilyname);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
    {
        NSLog(@"\tfont:'%@'",fontName);
    }
    NSLog(@"~~~~~~~~");
}

Might be above step and suggestion is helpful in your case:

Solution 3

While it appears that UIFont's fontNamed:size: method is fairly forgiving with the names you provide, the official fontName has always been the font's internal PostScript name. A font's PostScript name cannot contain spaces and usually has them replaced by a hyphen. I don't recall offhand, but I believe they may also be limited to 31 characters.

Determining what name you should use in code is extremely simple. Double click on the font you want to use on your iOS device to open it in Font Book. If the font is not already installed, click the Install button in the sample window that appears. When it's installed, select the font in the Font list. In Font Book's menu, choose Preview > Show Font Info. That will show info about the font like shown in the image below:

enter image description here

As you can see, the PostScript name for Helvetica Neue LT Std 65 Medium is HelveticaNeueLTStd-Md. That's the name you should use in code.

In your Info.plist file, under fonts for the application, you need to use the actual filename of the font itself, whatever that happens to be. In my case, it was HelveticaNeueLTStd-Md.otf.

Solution 4

Easier way to get the name of the font

  1. Right-click on your .ttf/.otf file and choose "Get Info"
  2. Read the Full Name property and use that as the name of the font

It works both in the info .plist file (adding the extension) and in the code with [UIFont fontWithName:<Full Name> size:17]

Solution 5

Just NSLog The font you want get:

NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);

And you get the array:

(
  "AmericanTypewriter-CondensedLight",
  "AmericanTypewriter-Light",
  "AmericanTypewriter-Bold",
  AmericanTypewriter,
  "AmericanTypewriter-CondensedBold",
  "AmericanTypewriter-Condensed"
)

Use any you need in Code any of font names AmericanTypewriter-Bold as [UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]

Also you could get All Applied Fonts grouped by Font Family

for (NSString* family in [UIFont familyNames])
{
    NSLog(@"%@", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family])
    {
        NSLog(@"  %@", name);
    }
}
Share:
52,188
Muhammad Umar
Author by

Muhammad Umar

Updated on November 06, 2021

Comments

  • Muhammad Umar
    Muhammad Umar over 2 years

    I have two fonts to add in my app for using.

    Here is the font images. Currently the files are named as

    name.font       = [UIFont fontWithName:@"Helvetica Neue LT Pro-Medium" size:10];
    headline.font   = [UIFont fontWithName:@"Helvetica Neue LT Pro-Light" size:8];
    

    putting same name in the Font Avaliable option in plist file.

    I have also tried adding file names like

    HelveticaNeueLTPro-Lt
    HelveticaNeueLTPro-Md
    

    but nothing seems to work. How can i get the exact name of the fonts.

    enter image description here