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

时间:2022-04-18 07:01:37

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?

如何找到要使用的名称?

14 个解决方案

#1


43  

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

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

  • Add your_new_font.ttf or your_new_font.otf to your Xcode project
  • 将your_new_font.ttf或your_new_font.otf添加到Xcode项目中
  • 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')
  • 在项目的info.plist中,为your_new_font.ttf或your_new_font.otf添加一个新条目到UIAppFonts数组(这个的纯文本是'由应用程序提供的字体')
  • 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:
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!
  • 在调试输出中,查找新字体的“family”和“font”名称。将显示为与新字体系列对应的“字体”名称(可能有多个与您的新字体“系列”关联的“字体”)传递给UIFont * myNewFont = [UIFont fontWithName:@“font_name_from_debug_output”尺寸:你应该做生意!

#2


109  

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.

我刚才使用的字体与Get Info中的“Full Name”名称不同。

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:

Swift 3.0代码:

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

#3


26  

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中使用字体,您必须根据字体的全名(PostScript名称)加载字体,该字体有时(通常是)与字体的实际文件名不同。

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-scan utility from fontconfig which is probaly already installed, but if not you can install it via homebrew: brew install fontconfig):

如果您使用的是Mac或Linux,只需从您拥有自定义字体的目录中的命令行运行此脚本(使用fontconfig中的fc-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/Fonts directory:

以下是在〜/ Library / Fonts目录中运行的上述命令的屏幕截图:

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

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.

上面的脚本将运行当前目录中的所有.ttf和.otf文件,然后打印出可用于在XCode或其他地方引用字体文件的每种字体的PostScript名称。

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 "\033[36m PostScript Name:\033[0m %s \e[90m(%s)\033[0m\n" "$postscriptname" "$file";
done

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

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

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

Hope this helps!

希望这可以帮助!

#4


19  

  • Install the font
  • 安装字体
  • Open Font Book app on your Mac
  • 在Mac上打开Font Book应用程序
  • Select the font and click on 'info' button
  • 选择字体并单击“信息”按钮
  • The name you're looking for is PostScript name
  • 您正在寻找的名称是PostScript名称

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

#5


5  

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

希望它可以帮助你......

#6


5  

Swift 3.0

Swift 3.0

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

#7


4  

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.

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

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

#8


3  

Swift 1.2:

Swift 1.2:

    for familyName in UIFont.familyNames() {
        for fontName in UIFont.fontNamesForFamilyName(familyName as! String) {
            println("\(familyName) : \(fontName)")
        }
    }

#9


2  

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构造函数的名称。

#10


2  

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.

根据需要替换为您自己的throwable Error对象。

#11


1  

Log familyNames of font file and then access the fonts:

记录fontname的字体文件,然后访问字体:

// You can log all font family names suing **fontNamesForFamilyName**

NSLog(@" font name %@", [UIFont fontNamesForFamilyName:@"the file name"]);

Hope it helps you.

希望它能帮到你。

#12


1  

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, you can see the fonts used with "internal" names if you search for the "customFonts" tag:

另一个“快速,实用”的黑客攻击,不涉及扫描模拟器或设备上存在的所有默认字体,就是在故事板中使用该字体。然后,如果使用文本编辑器编辑故事板,则可以在搜索“customFonts”标记时看到与“内部”名称一起使用的字体:

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

#13


0  

List font names and families for macOS with Swift 4.1:

使用Swift 4.1列出macOS的字体名称和系列:

NSFontManager.shared.availableFonts.map { print($0) }
NSFontManager.shared.availableFontFamilies.map { print($0) }

#14


-1  

Swift 4.0 Supported

Swift 4.0支持

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)})})
}

#1


43  

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

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

  • Add your_new_font.ttf or your_new_font.otf to your Xcode project
  • 将your_new_font.ttf或your_new_font.otf添加到Xcode项目中
  • 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')
  • 在项目的info.plist中,为your_new_font.ttf或your_new_font.otf添加一个新条目到UIAppFonts数组(这个的纯文本是'由应用程序提供的字体')
  • 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:
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!
  • 在调试输出中,查找新字体的“family”和“font”名称。将显示为与新字体系列对应的“字体”名称(可能有多个与您的新字体“系列”关联的“字体”)传递给UIFont * myNewFont = [UIFont fontWithName:@“font_name_from_debug_output”尺寸:你应该做生意!

#2


109  

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.

我刚才使用的字体与Get Info中的“Full Name”名称不同。

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:

Swift 3.0代码:

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

#3


26  

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中使用字体,您必须根据字体的全名(PostScript名称)加载字体,该字体有时(通常是)与字体的实际文件名不同。

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-scan utility from fontconfig which is probaly already installed, but if not you can install it via homebrew: brew install fontconfig):

如果您使用的是Mac或Linux,只需从您拥有自定义字体的目录中的命令行运行此脚本(使用fontconfig中的fc-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/Fonts directory:

以下是在〜/ Library / Fonts目录中运行的上述命令的屏幕截图:

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

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.

上面的脚本将运行当前目录中的所有.ttf和.otf文件,然后打印出可用于在XCode或其他地方引用字体文件的每种字体的PostScript名称。

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 "\033[36m PostScript Name:\033[0m %s \e[90m(%s)\033[0m\n" "$postscriptname" "$file";
done

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

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

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

Hope this helps!

希望这可以帮助!

#4


19  

  • Install the font
  • 安装字体
  • Open Font Book app on your Mac
  • 在Mac上打开Font Book应用程序
  • Select the font and click on 'info' button
  • 选择字体并单击“信息”按钮
  • The name you're looking for is PostScript name
  • 您正在寻找的名称是PostScript名称

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

#5


5  

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

希望它可以帮助你......

#6


5  

Swift 3.0

Swift 3.0

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

#7


4  

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.

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

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

#8


3  

Swift 1.2:

Swift 1.2:

    for familyName in UIFont.familyNames() {
        for fontName in UIFont.fontNamesForFamilyName(familyName as! String) {
            println("\(familyName) : \(fontName)")
        }
    }

#9


2  

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构造函数的名称。

#10


2  

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.

根据需要替换为您自己的throwable Error对象。

#11


1  

Log familyNames of font file and then access the fonts:

记录fontname的字体文件,然后访问字体:

// You can log all font family names suing **fontNamesForFamilyName**

NSLog(@" font name %@", [UIFont fontNamesForFamilyName:@"the file name"]);

Hope it helps you.

希望它能帮到你。

#12


1  

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, you can see the fonts used with "internal" names if you search for the "customFonts" tag:

另一个“快速,实用”的黑客攻击,不涉及扫描模拟器或设备上存在的所有默认字体,就是在故事板中使用该字体。然后,如果使用文本编辑器编辑故事板,则可以在搜索“customFonts”标记时看到与“内部”名称一起使用的字体:

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

#13


0  

List font names and families for macOS with Swift 4.1:

使用Swift 4.1列出macOS的字体名称和系列:

NSFontManager.shared.availableFonts.map { print($0) }
NSFontManager.shared.availableFontFamilies.map { print($0) }

#14


-1  

Swift 4.0 Supported

Swift 4.0支持

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)})})
}