使用Swift将自定义字体添加到macOS应用程序 [英] Adding a custom font to macOS app using Swift
问题描述
我跟着一堆教程,但它不工作:我只是想添加一个自定义的字体到一个macOS应用程序。
我基本上试过:
- 将
.ttf
字体文件添加到我的项目中:目标成员已设置,并且我还确保使用Bundle Phases
中的Copy Files
复制文件。编译后,我可以看到所有文件都在Bundle中。所以这似乎工作得很好。
- start =2>
-
Info.plist
:I >由应用程序提供的字体 ,并为每个要添加的字体文件创建一个项目(像myFont.tff
的值)。 我确信我使用了正确的字体名称。我在我的系统上安装了这些字体,并使用Interface Builder配置了一个标签,以便使用所需的字体。我打印它的值 - Added the
.ttf
font-files to my project: Target Membership is set and I also made sure that the files are copied usingCopy Files
withinBundle Phases
. After compiling I can see that all files are within the Bundle. So that seems to work perfectly fine. Info.plist
: I addedFonts provided by application
and created an item for every font-file I want to add (values likemyFont.tff
).I made sure that I use the correct font name. I installed the fonts on my system and configured a Label with Interface Builder so that it uses the desired font. I printed it’s value
print(myLabel.font.fontName)
.Confusing: If the exactly same font-file I want to add to the app is installed in the systems Fontbook and activated, everything works well. When I deactivate it, it doesn’t work. For me that indicates that I am using the correct font name.
I found
ATSApplicationFontsPath
and tried to add it to theInfo.plist
, but neither using a path (recommended by the docs) or values like.
(which seemed to work for some people out there) worked out.
print(myLabel.font.fontName)
。 混淆:如果完全相同字体文件我想添加到应用程序安装在系统Fontbook和激活,一切正常。当我停用它时,它不起作用。我发现 ATSApplicationFontsPath
,并尝试使用正确的字体名称。 将它添加到 Info.plist
中,但是既不使用路径(文档推荐),也不使用 / code>(这似乎适用于那里的一些人)。
$ b $ p
感谢任何帮助! 我偶然解决了我的问题。我注意到,在Bundle文件中,我的字体被包含了两次:它们被复制到Resources目录以及我在Copy文件建立阶段。
我从项目中删除了字体,再次添加(不检查目标成员),并将它们添加到构建阶段中,以便将它们复制到子路径中。
补充说明:
原来,提供的字体应用程序
是一个仅用于iOS的属性,所以它不需要用于macOS实现。
另外, ATSApplicationFontsPath
不需要任何斜杠。例如: Fonts
应该可以正常工作。
我很难找到.ttf字体的正确名称文件,有时会发现一些奇怪的行为:NSFont从字体家族中随机挑选字体(常规,中等,粗体...),而不进行任何代码更改。通过使用.otf文件,我也可以解决这个问题。
I followed a bunch of tutorials but it doesn’t work: I simply want to add a custom font to a macOS app.
What I tried essentially:
Appreciate any help!
I solved my problem by accident.
I noticed that within the Bundle files, my fonts were included twice: They were copied to the "Resources" directory as well as to the subpath I declared in the "Copy Files" Build Phases. I removed the fonts from my project, added them again (without checking the "Target Membership") and added them in the Build Phases so they were copied just to the subpath. After that it worked.
Additional notes:
Turned out that Fonts provided by application
is a iOS only property so it is not needed for an macOS implementation.
Also, ATSApplicationFontsPath
does not need any slashes. For example: Fonts
should work just fine.
I had a difficult time finding the correct names for .ttf font files and sometimes discovered some strange behaviour: NSFont picked randomly fonts (regular, medium, bold...) from a font family without any code changes. By using .otf files instead I could solve that too.
这篇关于使用Swift将自定义字体添加到macOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!