Adding a custom font to macOS app using Swift

前端 未结 5 2031
梦谈多话
梦谈多话 2021-02-20 10:46

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 .
相关标签:
5条回答
  • 2021-02-20 11:27

    THe value ATSApplicationFontsPath (i.e. Application fonts resource path) is relative to the Resources folder of your app bundle.

    To see where your fonts are copied to:

    1. In Xcode, expand the Products section under Project Navigator and click Show in Finder

    1. In Finder, Ctrl-click on your app and click Show Resource Contents. Browse to the Resources folder and find your fonts there.
    0 讨论(0)
  • 2021-02-20 11:32

    I had a hard time getting this to work based on the previous answers, so I decided to write up an answer myself. This is based on previous answers by @ixany and @rob-keniger.

    1. Create a folder named Fonts

    1. Add fonts to the Fonts folder. Uncheck Add to targets and check Copy items if needed.

    1. Add Application fonts resource path to Info.plist and enter Fonts.

    1. Go to Build Phases and create a New Copy Files Phase.

    1. Set the Destinations to Resources and Subpath to Fonts. Then add your font files to the list.

    0 讨论(0)
  • 2021-02-20 11:35

    None of the above worked for me in Xcode 12 beta 1 running BigSur but these steps did.

    1. Create a Fonts folder for organization.
    2. Add your fonts to the project. Be sure to check the macOS target!
    3. Set Application fonts resource path to "." (no quotes).

    No need to edit build phases or anything else.

    0 讨论(0)
  • 2021-02-20 11:37

    Xcode 12 to add custom fonts you have to exclude extension name from the string provided.

    i.e.

    .font(.custom("your font name", size:xx)) // works every time

    .font(.custom("your font name.extension",size:xx)) // always fails

    same is not true with Xcode 11.xx there fontname.extension is working.

    0 讨论(0)
  • 2021-02-20 11:42

    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.

    0 讨论(0)
提交回复
热议问题