xcodemacosswiftcustom-font

How to use custom fonts in a mac application?


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)
}

Solution

  • 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