xcode 如何在 Mac 应用程序中使用自定义字体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27680893/
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-09-15 06:25:41  来源:igfitidea点击:

How to use custom fonts in a mac application?

xcodemacosswiftcustom-font

提问by C-Viorel

I try to use custom fonts in my swift app, but they don't load.

我尝试在我的 swift 应用程序中使用自定义字体,但它们没有加载。

I copy the fonts.ttf in my resources folder, and I added the names in Info.plist under "Fonts provided by application " key.

我将 fonts.ttf 复制到我的资源文件夹中,并在“应用程序提供的字体”键下的 Info.plist 中添加了名称。

I've try with "Application fonts resource path" key from .plist , but no results. Here is the code I used to apply my font. I've try with : "MyFont.ttf" , and "MyFont"

我尝试使用 .plist 中的“应用程序字体资源路径”键,但没有结果。这是我用来应用我的字体的代码。我试过: "MyFont.ttf" 和 "MyFont"

@IBOutlet weak var label:NSTextField!

override func awakeFromNib() {
    label.font = NSFont(name: "MyFont.ttf", size: 15)
}

采纳答案by Leo Dabus

First add the desired font you want to embed to your OSX app to your project:

首先将您想要嵌入到您的 OSX 应用程序的所需字体添加到您的项目中:

enter image description here

在此处输入图片说明

Then click project > Info, then click the plus sign and add a new key "Application fonts resource path" and type the name of your fonts there creating an array of strings:

然后单击项目 > 信息,然后单击加号并添加一个新键“应用程序字体资源路径”并在其中键入字体名称以创建字符串数组:

enter image description here

在此处输入图片说明

Now you can select custom font and the name of the font will show there, you still need to use the Font Book to make it available inside Xcode.

现在您可以选择自定义字体,字体名称将显示在那里,您仍然需要使用 Font Book 使其在 Xcode 中可用。

enter image description here

在此处输入图片说明

回答by Chris Payne

As "Application fonts resource path" is now a string type in XCode 7.3.1 and I couldn't seem to find a way to use an Array for multiple fonts, I used "." in the Info.plist:

由于“应用程序字体资源路径”现在是 XCode 7.3.1 中的字符串类型,我似乎无法找到将数组用于多种字体的方法,因此我使用了“。” 在 Info.plist 中:

     Application fonts resource path     String    .

and this seemed to work to pick up all my custom fonts in a Resources folder dynamically e.g. using Swift

这似乎可以动态地在资源文件夹中获取我所有的自定义字体,例如使用 Swift

 labelText.font = NSFont(name: "DS-Digital", size: 48)

However, to see it in XCode design mode (to choose a font from drop-down menu), I needed to first add the font to Font Book.

但是,要在 XCode 设计模式下查看它(从下拉菜单中选择一种字体),我需要先将字体添加到 Font Book。

However, Font Book was not required for the dynamic method to work :)

但是,动态方法不需要 Font Book :)

回答by Andrew

ATSApplicationFontsPathis for macOS:

ATSApplicationFontsPath适用于 macOS

ATSApplicationFontsPath(String- macOS) identifies the location of a font file or directory of fonts in the bundle's Resources directory. If present, macOS activates the fonts at the specified path for use by the bundled app. The fonts are activated only for the bundled app and not for the system as a whole. The path itself should be specified as a relative directory of the bundle's Resources directory. For example, if a directory of fonts was at the path /Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/, you should specify the string Stuff/MyFonts/ for the value of this key.

ATSApplicationFontsPath( String- macOS) 标识字体文件或字体目录在包的资源目录中的位置。如果存在,macOS 会在指定路径激活字体以供捆绑的应用程序使用。字体仅为捆绑的应用程序激活,而不是为整个系统激活。路径本身应指定为包资源目录的相对目录。例如,如果字体目录位于路径 /Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/ 中,您应该为此键的值指定字符串 Stuff/MyFonts/。

macOS app Instructions:

macOS 应用说明:

  1. Select your Xcode project in the project navigator
  2. Select your app target
  3. Click the + buttonand to add a New Copy Files Phase
  4. Select Resourcesfor the destination
  5. Under subpathspecify the directory (e.g. Fonts) where your embedded fonts will be copied to within your application bundle's Resources directory.
  6. Drag and drop the font files into the file list of the Copy Filesbuild phase.
  1. 项目导航器中选择您的 Xcode 项目
  2. 选择您的 应用目标
  3. 单击+ 按钮并添加一个新的复制文件阶段
  4. 选择资源目的地
  5. subpath 下指定目录(例如Fonts),您的嵌入字体将被复制到应用程序包的 Resources 目录中。
  6. 将字体文件拖放到Copy Files构建阶段的文件列表中。

UIAppFontsis for iOS:

UIAppFonts适用于 iOS

UIAppFonts(Array- iOS) Specifies any app-provided fonts that should be made available through the normal mechanisms. Each item in the array is a string containing the name of a font file (including filename extension) that is located in the app's bundle. The system loads the specified fonts and makes them available for use by the app when that app is run.

This key is supported in iOS 3.2 and later.

UIAppFonts( Array- iOS) 指定应通过正常机制提供的任何应用程序提供的字体。数组中的每一项都是一个字符串,其中包含位于应用程序包中的字体文件(包括文件扩展名)的名称。系统加载指定的字体,并在应用程序运行时使它们可供应用程序使用。

iOS 3.2 及更高版本支持此密钥。

回答by Mike.P

For those whose font family is called something like My-Custom-Font-Family: be aware that in code you should instantiate your custom font like this: NSFont(name: "MyCustomFontFamily-Bold", size: 20)Spaces and "-" are ignored and font type is written after "-". I did not see this in any docs and spend a few hours trying to figure out wtf was wrong. Also if you want to get list of all available fonts you can use this code

对于那些字体系列被称为My-Custom-Font-Family 的人:请注意,在代码中你应该像这样实例化你的自定义字体:NSFont(name: "MyCustomFontFamily-Bold", size: 20)空格和“-”被忽略,字体类型写在“-”之后。我没有在任何文档中看到这一点,并花了几个小时试图找出 wtf 是错误的。此外,如果您想获取所有可用字体的列表,您可以使用此代码

for font in NSFontManager.shared.availableFonts {
    print(font)
}

回答by Name

Try doing this from the interface builder, in the attributes inspector .

尝试从界面构建器的属性检查器中执行此操作。

回答by Kelin Sasha

By directly setting the Application fonts resource pathas my font file's name, I solved this problem by sheer luck.

通过直接将应用程序字体资源路径设置为我的字体文件名,我完全靠运气解决了这个问题。

1

1