16

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.

enter image description here

  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.

enter image description here

Appreciate any help!

ixany
  • 5,433
  • 9
  • 41
  • 65
  • If you are thinking that you can use a custom font by just regestering it with Info.plist just as you do in iOS, no, that's how it works in Cocoa. – El Tomato Jan 21 '17 at 23:23
  • 12
    @ElTomato You don’t lead me into the right direction saying how it doesn’t work. – ixany Jan 22 '17 at 08:43

5 Answers5

21

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

Pætur Magnussen
  • 901
  • 1
  • 11
  • 24
13

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.

ixany
  • 5,433
  • 9
  • 41
  • 65
  • 1
    Thanks man! Saved my day. I've been testing various solutions all day, but in the end, in my case, my app is multiplatform (iOS and macOS) and I had to add the fonts in the mac bundle, it does not work in the shared bundle. – Daniel Lima Oct 16 '18 at 19:29
  • 1
    thanks ixany! you have also saved my day but there is one problem that I am not able to access the added fonts in storyboard. Please help me out otherwise I need to set all fonts programmatic. – Ravindra Kumar Sonkar May 11 '20 at 06:18
3

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.

Kory Heard
  • 41
  • 1
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:

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

Locate app bundle

  1. In Finder, Ctrl-click on your app and click Show Resource Contents. Browse to the Resources folder and find your fonts there. enter image description here
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • I made sure every font file is copied to the Bundles directory. I tried `ATSApplicationFontsPath` leaving empty or with a value like `fonts/` but it doesn’t work. – ixany Jan 22 '17 at 08:42
  • Hard to see where things went wrong so I'm just giving some generic tips: (a) make sure your fonts are indeed copied to `Resources/fonts`; (b) try `fonts` instead of `fonts/` in the Info.plist file – Code Different Jan 23 '17 at 04:43
  • (a) The files are copied correctly into `Contents/Resources/Fonts`, (b) Tried different ways but nothing seemed to work (`/Fonts`, `Fonts/`, `Fonts`) – ixany Jan 23 '17 at 09:11
  • Did you found solution? I'm also facing same issue taking lot of time. – iMash Jan 13 '21 at 06:41
  • Same issue here. I still can't get this to works. (I'm using the font from Storyboard.) – Jinyu Meng Feb 22 '21 at 13:40
1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Surepic
  • 13
  • 5