如何在MacOS中以编程方式在Mac中安装自定义字体 [英] How to install custom font in mac programmatically in macOS

查看:149
本文介绍了如何在MacOS中以编程方式在Mac中安装自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Mac应用程序,其中具有自定义字体,而我的Mac上的应用程序文件夹的自定义字体簿中没有该自定义字体.

因此,当应用启动时,我使用以下代码检查了是否在Mac中安装了字体.

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

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

现在,如果尚未安装该字体,该如何以编程方式安装它?

解决方案

  1. 将字体ttf文件添加到您的应用程序包中,例如在 内容/资源/字体/
  2. 在您的info.plist中添加密钥:ATSApplicationFontsPath和字符串:Fonts

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

这是一个相关的堆栈溢出答案: https://stackoverflow.com/a/29003792/3342547

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, e.g. 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

这篇关于如何在MacOS中以编程方式在Mac中安装自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆