ios 如何从 otf 或 ttf 文件中获取字体名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16788330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:42:22  来源:igfitidea点击:

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

iosuifont

提问by Fogmeister

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...

文件名是“ProximaNova-Regular.otf”并加载我刚刚使用的字体...

[UIFont fontWithName:@"ProximaNova-Regular" size:20];

This worked perfectly.

这工作得很好。

Now in this new app I have three font files...

现在在这个新应用程序中,我有三个字体文件...

Dude_Willie.otf
Impact
handsean.ttf

But I'm not sure how to load these.

但我不确定如何加载这些。

I have tried

我试过了

[UIFont fontWithName:<the file name> size:20];

But this just falls back to using Helvetica.

但这只是退回到使用 Helvetica。

How can I find what name to use?

如何找到要使用的名称?

采纳答案by John Jacecko

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

按照以下四个简单步骤在您的 iOS 应用程序中添加和使用新字体:

  • Add your_new_font.ttfor your_new_font.otfto your Xcode project
  • In your project's info.plist, add a new entry for your_new_font.ttfor your_new_font.otfto the UIAppFontsarray (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:
  • your_new_font.ttfyour_new_font.otf添加到您的 Xcode 项目中
  • 在您项目的 info.plist 中,将your_new_font.ttfyour_new_font.otf的新条目添加到UIAppFonts数组中(该条目的纯文本是“应用程序提供的字体”)
  • 在这一点上,我建议添加这个临时的调试代码块来转储您的应用程序可以访问的所有字体,包括您新添加的 your_new_font:

//Swift

//迅速

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

//Objective-c

//目标-c

for(NSString *fontfamilyname in [UIFont familyNames]) {
    NSLog(@"family:'%@'",fontfamilyname);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) {
        NSLog(@"\tfont:'%@'",fontName);
    }
    NSLog(@"-------------");
}
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!
  • 在调试输出中,查找新字体的“系列”和“字体”名称。将显示为与您的新字体系列相对应的“字体”名称的任何内容(可能有多个与您的新字体“系列”相关联的“字体”)传递给UIFont *myNewFont = [UIFont fontWithName:@"font_name_from_debug_output" size:20]您,您应该可以开展业务了!

回答by Segev

Right click on the TTF -> Get Info

右键单击 TTF -> 获取信息

"Full Name" is what you're looking for.

“全名”就是您要查找的内容。

That's what worked for me with TTFs.

这就是对我有用的 TTF。

Edit:

编辑:

I just used a font that had a different name from the "Full Name" in Get Info.

我只是使用了一种与“获取信息”中的“全名”名称不同的字体。

For the compilation of this answer, If the quick check above doesn't work, run this code in your project:

对于此答案的编译,如果上面的快速检查不起作用,请在您的项目中运行此代码:

for (NSString *fontFamilyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
        NSLog(@"Family: %@    Font: %@", fontFamilyName, fontName);
    }
}

And search for the correct name of the font you want to use.

并搜索您要使用的字体的正确名称。

Swift 3.0 code:

斯威夫特 3.0 代码:

for fontFamilyName in UIFont.familyNames{
    for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){
        print("Family: \(fontFamilyName)     Font: \(fontName)")
    }
}

回答by Elin Gersin

  • Install the font
  • Open Font Book app on your Mac
  • Select the font and click on 'info' button
  • The name you're looking for is PostScript name
    • MacOS: View -> Show Font Info
  • 安装字体
  • 在 Mac 上打开 Font Book 应用
  • 选择字体并单击“信息”按钮
  • 您要查找的名称是PostScript 名称
    • MacOS:查看 -> 显示字体信息

example

例子

回答by radiovisual

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.

要在 iOS 中使用字体,您必须根据字体的FULL NAME(PostScript 名称)加载字体,该名称有时(通常是)与字体的实际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".

想象自己将字体文件“Arial-regular.ttf”重命名为“foo.ttf”。您刚刚重命名的字体文件中包含的字体仍然是“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-scanutility from fontconfigwhich is probaly already installed, but if not you can install it via homebrew: brew install fontconfig):

如果您使用的是 Mac 或 Linux,只需从您拥有自定义字体的目录中的命令行运行此脚本(使用可能已经安装的fontconfigfc-scan实用程序,但如果没有,您可以通过自制软件安装它:):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/Fontsdirectory:

这是在我的~/Library/Fonts目录上运行的上述命令的屏幕截图:

enter image description here

在此处输入图片说明

The script above will run through all the .ttfand .otffiles in the current directory, then print out the PostScript Namefor each font which you can use to reference the font file in XCode or elsewhere.

上面的脚本将遍历当前目录中的所有.ttf.otf文件,然后打印出每种字体的PostScript 名称,您可以使用它来引用 XCode 或其他地方的字体文件。

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

如果您想了解一些附加信息(PostScriptName、Filename)和一些颜色编码,您可以运行以下替代脚本:

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

enter image description here

在此处输入图片说明

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.

这比在 AppDelegate.m 文件中复制粘贴代码来打印名称每次要添加新字体文件时要快一点,这是流行的方法,也比在 FontBook 中打开字体要快一些检查 PostScript 名称。

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 fontnamesso all I have to do is type fontnamesat 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.

有用的提示:如果您在终端中为上述脚本设置别名,那么您需要做的就是输入一个命令来获取当前目录中所有文件的所有 PostScript 字体名称(我的函数被调用,fontnames所以我要做的就是fontnames在带有字体的目录内的终端上键入,PostScript 名称将自动打印,然后您将节省开发工作流程中的时间,并在需要时准备好使用这个方便的脚本。

Hope this helps!

希望这可以帮助!

回答by Arpit Kulshrestha

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...

希望对你有帮助...

回答by Sekhar22

Swift 3.0

斯威夫特 3.0

for familyName in UIFont.familyNames {
        for fontName in UIFont.fontNames(forFamilyName: familyName ) {
            print("\(familyName) : \(fontName)")
        }
    }

回答by Robert

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 Errorobjects as required.

Error根据需要替换为您自己的可投掷对象。

回答by JaredH

Swift 4.0+solution

Swift 4.0+解决方案

    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

家族:Apple SD Gothic Neo

字体:AppleSDGothicNeo-Thin

字体:AppleSDGothicNeo-Light

回答by Oliver Pearmain

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.

将字体添加到项目/应用程序后,添加此代码(可能只是在应用程序委托 didFinishLaunchingWithOptions 方法中)以打印出应用程序的所有可用字体。从该列表中,您应该能够识别您所追求的字体。之后不要忘记删除不必要的代码。

for (NSString *fontFamilyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
        NSLog(@"Family: %@    Font: %@", fontFamilyName, fontName);
    }
}

回答by Moritz

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.

您还可以使用 otfinfo --info arial.ttf 从命令行获取名称。postscript 名称是您需要传递给 UIFont 构造函数的名称。