简体   繁体   中英

How to install custom font in mac programmatically in macOS

I am having a mac app in which I have custom font which is not there in my custom font book of application folder in my mac.

So, when an app launches, I checked it that if a font is installed in mac or not with the below code.

NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies];

if ([fonts containsObject:@"Square721 BT"])
{
    NSLog(@"Fonts Available");
}
else
{
    NSLog(@"No fonts");
}

Now, if the font is not already installed, how can I install it programmatically?

  1. Add your font ttf file to your application bundle, eg in Contents/Resources/Fonts/
  2. In your info.plist add the key: ATSApplicationFontsPath and string: Fonts

See: https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/GeneralPurposeKeys.html#//apple_ref/doc/uid/TP40009253-SW8

And here's a relevant Stack Overflow answer: https://stackoverflow.com/a/29003792/3342547

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM