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

前端 未结 15 1590
-上瘾入骨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:56

    You want to know how to get name go for this :-

      NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
    
      for (NSInteger indFamily=0; indFamily<[familyNames count]; ++indFamily) 
      {
            NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    
            NSArray *fontNames = [[NSArray alloc] initWithArray:
                  [UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];
    
            for (NSInteger indFont=0; indFont<[fontNames count]; ++indFont) 
            {
                  NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
            }
      }
    

    hope it helps you...

    0 讨论(0)
  • 2020-11-28 03:56

    Another "quick, practical" hack that doesn't involve scanning through all the default fonts that exist on either the emulator or device is to use the font for something in your storyboard. Then if you edit the storyboard with a text editor (open as source code), you can see the fonts used with "internal" names if you search for the "customFonts" tag:

    <customFonts key="customFonts">
        <array key="Linotype - AvenirNextLTPro-Bold.otf">
            <string>AvenirNextLTPro-Bold</string>
        </array>
        <array key="Linotype - AvenirNextLTPro-Regular.otf">
            <string>AvenirNextLTPro-Regular</string>
        </array>
        <array key="TradeGothicLTPro-BdCn20.otf">
            <string>TradeGothicLTPro-BdCn20</string>
        </array>
    </customFonts>
    
    0 讨论(0)
  • 2020-11-28 03:58

    After you've added your fonts to your project/app, add this code (probably just in app delegate didFinishLaunchingWithOptions method) in order to print out all the available fonts for your app. From with-in that list you should be able to identify the font you're after. Don't forget to remove the unnecessary code after.

    for (NSString *fontFamilyName in [UIFont familyNames]) {
        for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
            NSLog(@"Family: %@    Font: %@", fontFamilyName, fontName);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:59

    Swift 3.0

    for familyName in UIFont.familyNames {
            for fontName in UIFont.fontNames(forFamilyName: familyName ) {
                print("\(familyName) : \(fontName)")
            }
        }
    
    0 讨论(0)
  • 2020-11-28 04:06

    Swift 4.0+ solution

        for fontFamilyName in UIFont.familyNames {
            print("family: \(fontFamilyName)\n")
    
            for fontName in UIFont.fontNames(forFamilyName: fontFamilyName) {
                print("font: \(fontName)")
            }
        }
    

    Will output:

    family: Apple SD Gothic Neo

    font: AppleSDGothicNeo-Thin

    font: AppleSDGothicNeo-Light

    0 讨论(0)
  • 2020-11-28 04:07

    Swift 4.0 Supported

    This is only one line code for print all font family and it's font's names

    override func viewDidLoad() {
        super.viewDidLoad()
        //Main line of code
        UIFont.familyNames.sorted().forEach({ print($0); UIFont.fontNames(forFamilyName: $0 as String).forEach({print($0)})})
    }
    
    0 讨论(0)
提交回复
热议问题