问题
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:
- Added the .ttffont-files to my project: Target Membership is set and I also made sure that the files are copied usingCopy FileswithinBundle Phases. After compiling I can see that all files are within the Bundle. So that seems to work perfectly fine.
- Info.plist: I added- Fonts provided by applicationand created an item for every font-file I want to add (values like- myFont.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 - ATSApplicationFontsPathand 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!
回答1:
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.
回答2:
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:
- In Xcode, expand the Products section under Project Navigator and click Show in Finder
- In Finder, Ctrl-click on your app and click Show Resource Contents. Browse to the Resourcesfolder and find your fonts there.
回答3:
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.
- Create a folder named Fonts
- Add fonts to the Fontsfolder. UncheckAdd to targetsand checkCopy items if needed.
- Add Application fonts resource pathto Info.plist and enterFonts.
- Go to Build Phasesand create aNew Copy Files Phase.
- Set the DestinationstoResourcesandSubpathtoFonts. Then add your font files to the list.
来源:https://stackoverflow.com/questions/41785440/adding-a-custom-font-to-macos-app-using-swift