How do I get the font name from an otf or ttf file?

前端 未结 15 1686
-上瘾入骨i
-上瘾入骨i 2020-11-28 03:44

I have used a custom font in my previous app.

The file name was \"ProximaNova-Regular.otf\" and to load the font I just used...

[UIFont fontWithName:         


        
15条回答
  •  青春惊慌失措
    2020-11-28 03:54

    Follow these four easy steps to add and use a new font in your iOS app:

    • Add your_new_font.ttf or your_new_font.otf to your Xcode project
    • In your project's info.plist, add a new entry for your_new_font.ttf or your_new_font.otf to the UIAppFonts array (plain text for this one is 'Fonts provided by application')
    • At this point, I recommend adding this temporary chunk of debug code to dump all fonts that are accessible by your app, including your newly added your_new_font:

    //Swift

        for family: String in UIFont.familyNames {
            print("\(family)")
            for names: String in UIFont.fontNames(forFamilyName: family) {
                print("== \(names)")
            }
        }
    

    //Objective-c

    for(NSString *fontfamilyname in [UIFont familyNames]) {
        NSLog(@"family:'%@'",fontfamilyname);
        for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) {
            NSLog(@"\tfont:'%@'",fontName);
        }
        NSLog(@"-------------");
    }
    
    • In the debug output, look for your new font's 'family' and 'font' name. Pass whatever is displayed as the 'font' name corresponding to your new font family (there could be more than one 'font' associated with your new font 'family') to UIFont *myNewFont = [UIFont fontWithName:@"font_name_from_debug_output" size:20] and you should be in business!

提交回复
热议问题