How to use custom fonts in a mac application?

C-Viorel picture C-Viorel · Dec 28, 2014 · Viewed 9.5k times · Source

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

Answer

Chris Payne picture Chris Payne · Aug 19, 2016

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