How to use custom fonts in a mac application?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 16:47:11

问题


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

回答1:


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

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:

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.




回答2:


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




回答3:


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.




回答4:


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




回答5:


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




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


来源:https://stackoverflow.com/questions/27680893/how-to-use-custom-fonts-in-a-mac-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!