22

I try to use custom fonts in my swift app, but they don't load.

I copy the fonts.ttf in my resources folder, and I added the names in Info.plist under "Fonts provided by application " key.

I've try with "Application fonts resource path" key from .plist , but no results. Here is the code I used to apply my font. I've try with : "MyFont.ttf" , and "MyFont"

@IBOutlet weak var label:NSTextField!

override func awakeFromNib() {
    label.font = NSFont(name: "MyFont.ttf", size: 15)
}
dckuehn
  • 2,427
  • 3
  • 27
  • 37
C-Viorel
  • 1,801
  • 3
  • 22
  • 41
  • From another SO answer (sorry can't find it now): the name string can be found by opening the font in Font Book and using the string next to `PostScript name` that's shown in the inspector pane. – Todd Nov 21 '18 at 17:04

6 Answers6

32

As "Application fonts resource path" is now a string type in XCode 7.3.1 and I couldn't seem to find a way to use an Array for multiple fonts, I used "." in the Info.plist:

     Application fonts resource path     String    .

and this seemed to work to pick up all my custom fonts in a Resources folder dynamically e.g. using Swift

 labelText.font = NSFont(name: "DS-Digital", size: 48)

However, to see it in XCode design mode (to choose a font from drop-down menu), I needed to first add the font to Font Book.

However, Font Book was not required for the dynamic method to work :)

Chris Payne
  • 321
  • 3
  • 4
16

ATSApplicationFontsPath is for macOS:

ATSApplicationFontsPath (String - macOS) identifies the location of a font file or directory of fonts in the bundle’s Resources directory. If present, macOS activates the fonts at the specified path for use by the bundled app. The fonts are activated only for the bundled app and not for the system as a whole. The path itself should be specified as a relative directory of the bundle’s Resources directory. For example, if a directory of fonts was at the path /Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/, you should specify the string Stuff/MyFonts/ for the value of this key.

macOS app Instructions:

  1. Select your Xcode project in the project navigator
  2. Select your app target
  3. Click the + button and to add a New Copy Files Phase
  4. Select Resources for the destination
  5. Under subpath specify the directory (e.g. Fonts) where your embedded fonts will be copied to within your application bundle's Resources directory.
  6. Drag and drop the font files into the file list of the Copy Files build phase.

UIAppFonts is for iOS:

UIAppFonts (Array - iOS) Specifies any app-provided fonts that should be made available through the normal mechanisms. Each item in the array is a string containing the name of a font file (including filename extension) that is located in the app’s bundle. The system loads the specified fonts and makes them available for use by the app when that app is run.

This key is supported in iOS 3.2 and later.

Andrew
  • 7,630
  • 3
  • 42
  • 51
  • i've tried almost everything and this is what actually worked for mac os. also it is the easiest solution. – Mec Os Jan 21 '20 at 04:35
7

By directly setting the Application fonts resource path as my font file's name, I solved this problem by sheer luck.

1

Kelin Sasha
  • 167
  • 1
  • 7
  • 2
    I just left it blank and it worked ATSApplicationFontsPath my fonts are in Resources root folder. – CiNN Jul 28 '20 at 17:39
6

For those whose font family is called something like My-Custom-Font-Family: be aware that in code you should instantiate your custom font like this: NSFont(name: "MyCustomFontFamily-Bold", size: 20) Spaces and "-" are ignored and font type is written after "-". I did not see this in any docs and spend a few hours trying to figure out wtf was wrong. Also if you want to get list of all available fonts you can use this code

for font in NSFontManager.shared.availableFonts {
    print(font)
}
Mike.P
  • 61
  • 1
  • 3
4

First add the desired font you want to embed to your OSX app to your project:

enter image description here

Then click project > Info, then click the plus sign and add a new key "Application fonts resource path" and type the name of your fonts there creating an array of strings:

enter image description here

Now you can select custom font and the name of the font will show there, you still need to use the Font Book to make it available inside Xcode.

enter image description here

AJcodez
  • 31,780
  • 20
  • 84
  • 118
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • I think i get crazy! I done this, exactly like you said , but , nothing . Added font is not present in font list. – C-Viorel Dec 29 '14 at 07:06
  • Can you send me the font link? The problem might be with the Font selected. Have you tried with another font ? – Leo Dabus Dec 29 '14 at 07:07
  • Do you need all 4 or just one of them ? – Leo Dabus Dec 29 '14 at 07:14
  • i need just one, but even in your project , font selected for label in attribute inspector is System 48 , if i try to find DS-Digital , i could't find. – C-Viorel Dec 29 '14 at 07:22
  • I think you need to add it using Font Book or just install them double-clicking the fonts to make it show there otherwise I think it can be done programmatically only – Leo Dabus Dec 29 '14 at 07:37
  • This procedure here doesn't add the Font to the system, it only makes the font available to the App itself – Leo Dabus Dec 29 '14 at 07:42
  • I have confirmed here and you have to install the font to your user and enable it before opening Xcode to have it available for you at the inspector. – Leo Dabus Dec 29 '14 at 08:26
  • After compiling the app you can close Xcode, disable the font at the Font Book and open your App you will see that it is using the embedded font. – Leo Dabus Dec 29 '14 at 08:28
  • In the end, with your help i did it. Thanks. – C-Viorel Dec 29 '14 at 13:16
  • 3
    Application fonts resource path is a string type and I can't add array of strings – Myat Min Soe Jun 11 '16 at 20:28
  • 2
    hi, what if Application fonts resource path is a string not a array, I couldn't add font like you did with that image – stan liu Sep 08 '16 at 04:38
  • 4
    For me this was the issue, the font MUST be installed in the system (ie. Font Book) before it can be embedded in the application. This seems ridiculous, but I'm assuming the explanation is so you can use it in Interface Builder, but even if you only create the font in code, it needs to be installed in Font Book initially, then you can remove it if you want to. And Apple does not mention any of that on the page for how to embed fonts in your app so it is very confusing. Also, for macOS, I'm using the plist key "Fonts Provided By Application" and not "Application fonts resource path". – Ben Stahl Mar 01 '18 at 19:41
  • @myatmins and stan liu, this seems like bug in Xcode 9.X. What I had to do was right-click in the blank area of the plist and select Property list type > None first. Then add the "Fonts provided by application" key, change the type to Array, add the strings for each font, then set the plist type back to default. – Ben Stahl Mar 01 '18 at 19:46
1

Try doing this from the interface builder, in the attributes inspector .

Name
  • 179
  • 1
  • 8
  • From IB i have access only to installed system fonts. I want to load my own font. – C-Viorel Dec 28 '14 at 21:57
  • 1
    In IB ,attributes inspector (font line) click the "T" and click font then custom. If you want (ex MyFont), first you have to loaded into OS X(Font Book) ,then you can select your font.Sorry for my bad english . – Name Dec 28 '14 at 22:05
  • but that resolve the problem just for me . if i install a .ttf font in fontBook i can use it very easy, but , then every user who use my app needs to install that font, and i am pretty sure about one thing : User is lazy. – C-Viorel Dec 28 '14 at 22:12
  • 1
    First you need to put your font in the project and use ATSApplicationFontsPath key in the info.plist .More info https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/GeneralPurposeKeys.html – Name Dec 28 '14 at 22:25
  • Application fonts resource path is the new key name for ATSApplicationFontsPath, so i already did that, but , is possible to do it wrong. – C-Viorel Dec 28 '14 at 22:30
  • Just put it in your app bundle(project) and Xcode will use it. – Name Dec 28 '14 at 22:33
  • @C-Viorel every user will not need to install the font; it will come with the app – Pranav Wadhwa Aug 19 '16 at 13:36