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

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

    To use fonts in iOS, you have to load the font based on the font's FULL NAME (PostScript Name), which is sometimes (and usually is) different from the font's actual FILE NAME.

    Imagine yourself renaming the Font file "Arial-regular.ttf" to be "foo.ttf". The font contained inside the font file you just renamed is still "Arial-regular".

    There are some good programmatic ways to get the font name already on this thread, but I have a different approach using the command line.

    If you are on a Mac or Linux, simply run this script from the command line in the directory where you have your custom fonts (uses the fc-scan utility from fontconfig which is probaly already installed, but if not you can install it via homebrew: brew install fontconfig):

    for file in "$arg"*.{ttf,otf}; do fc-scan --format "%{postscriptname}\n" $file; done
    

    Here is a screenshot of the above command running on my ~/Library/Fonts directory:

    The script above will run through all the .ttf and .otf files in the current directory, then print out the PostScript Name for each font which you can use to reference the font file in XCode or elsewhere.

    If you want to get fancy with some additional information (PostScriptName, Filename) and some color coding, you can run this alternative script:

    for file in "$arg"*.{ttf,otf}; do 
        postscriptname=$(fc-scan --format "%{postscriptname}\n" $file);
        printf "\033[36m PostScript Name:\033[0m %s \e[90m(%s)\033[0m\n" "$postscriptname" "$file";
    done
    

    This is a bit faster than copy-pasting code inside of your AppDelegate.m file to print out the names every time you want to add a new font file, which is the popular method, and it's also faster than opening the Font in FontBook to inspect the PostScript Name.

    USEFUL TIP: If you alias the above script in your terminal so that all you need to do is type a single command to get all the PostScript font names for all the files in the current directory (my function is called fontnames so all I have to do is type fontnames at the terminal inside the directory with fonts in it, and the PostScript names will be printed automatically, then you will save time in your development workflow and have this handy script ready to use when you need it.

    Hope this helps!

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

    You can also use otfinfo --info arial.ttf to get the name from command line. The postscript name is the one you need to pass to the UIFont constructor.

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

    If you want to find the font name for a given font file programmatically:

    import Foundation
    
    func loadFontName(for file: URL) throws -> String {
        let data = try Data(contentsOf: file)
    
        guard let provider = CGDataProvider(data: data as CFData) else {
            throw Error("Could not create data provider")
        }
    
        guard let font = CGFont(provider) else {
            throw Error("Could not load font")
        }
    
        guard let name = font.postScriptName else {
            throw Error("Could not get font name from font file")
        }
    
        return name as String
    }
    

    Replace with your own throwable Error objects as required.

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

    Swift 1.2:

        for familyName in UIFont.familyNames() {
            for fontName in UIFont.fontNamesForFamilyName(familyName as! String) {
                println("\(familyName) : \(fontName)")
            }
        }
    
    0 讨论(0)
  • 2020-11-28 03:53

    List font names and families for macOS with Swift 4.1:

    NSFontManager.shared.availableFonts.map { print($0) }
    NSFontManager.shared.availableFontFamilies.map { print($0) }
    
    0 讨论(0)
  • 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!
    0 讨论(0)
提交回复
热议问题