Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xcode - 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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...