How to use custom fonts in a Mac application?

I am trying to use custom fonts in my quick application, but they are not loading.

I will copy the fonts.ttf file in my resources folder, and I have added the names to Info.plist in the "Fonts provided by the application" section.

I tried the key "resource path" from .plist, but no results. Here is the code I used to apply my font. I tried: "MyFont.ttf" and "MyFont"

@IBOutlet weak var label:NSTextField! override func awakeFromNib() { label.font = NSFont(name: "MyFont.ttf", size: 15) } 
+6
source share
4 answers

First add the desired font that you want to embed in your OSX application in your project:

enter image description here

Then click Project> Information, then click the plus sign and add a new key to the Application Resource Path and enter the name of your fonts, creating an array of strings:

enter image description here

Now you can choose your own font, and the font name will be displayed there, you still need to use the Font Book to make it available inside Xcode.

enter image description here

+3
source

Since the "path to application font resources" is now a string type in Xcode 7.3.1, and I could not find a way to use the array for multiple fonts, I used ".". in Info.plist:

  Application fonts resource path String . 

and it seemed to work to raise all my custom fonts in the Resources folder dynamically, for example. using swift

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

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

However, the dynamic method does not require a font book :)

+17
source

Try to do this from the interface constructor in the attribute inspector.

+1
source

Directly setting the Application fonts resource path as the font file name, I solved this problem by pure chance.

1

0
source

Source: https://habr.com/ru/post/980235/


All Articles