使用Swift将自定义字体添加到macOS应用程序 [英] Adding a custom font to macOS app using Swift

查看:264
本文介绍了使用Swift将自定义字体添加到macOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着一堆教程,但它不工作:我只是想添加一个自定义的字体到一个macOS应用程序。



我基本上试过:


  1. .ttf 字体文件添加到我的项目中:目标成员已设置,并且我还确保使用 Bundle Phases 中的 Copy Files 复制文件。编译后,我可以看到所有文件都在Bundle中。所以这似乎工作得很好。

    start =2>
  1. Info.plist :I >由应用程序提供的字体 ,并为每个要添加的字体文​​件创建一个项目(像 myFont.tff 的值)。

  2. 我确信我使用了正确的字体名称。我在我的系统上安装了这些字体,并使用Interface Builder配置了一个标签,以便使用所需的字体。我打印它的值 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:

    1. Added the .ttf font-files to my project: Target Membership is set and I also made sure that the files are copied using Copy Files within Bundle Phases. After compiling I can see that all files are within the Bundle. So that seems to work perfectly fine.

    1. Info.plist: I added Fonts provided by application and created an item for every font-file I want to add (values like myFont.tff).

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

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

    4. I found ATSApplicationFontsPath and tried to add it to the Info.plist, but neither using a path (recommended by the docs) or values like . (which seemed to work for some people out there) worked out.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆