如何在MacOS中以编程方式在Mac中安装自定义字体

编程入门 行业动态 更新时间:2024-10-25 04:16:39
本文介绍了如何在MacOS中以编程方式在Mac中安装自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies]; if ([fonts containsObject:@"Square721 BT"]) { NSLog(@"Fonts Available"); } else { NSLog(@"No fonts"); }

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

解决方案

  • 将字体ttf文件添加到您的应用程序包中,例如在 内容/资源/字体/
  • 在您的info.plist中添加密钥:ATSApplicationFontsPath和字符串:Fonts
  • 请参阅: developer.apple/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/GeneralPurposeKeys.html#//apple_ref/doc/uid/TP40009253-SW8

    这是一个相关的堆栈溢出答案: stackoverflow/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?

    解决方案

  • Add your font ttf file to your application bundle, e.g. in Contents/Resources/Fonts/
  • In your info.plist add the key: ATSApplicationFontsPath and string: Fonts
  • See: developer.apple/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/GeneralPurposeKeys.html#//apple_ref/doc/uid/TP40009253-SW8

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

    更多推荐

    如何在MacOS中以编程方式在Mac中安装自定义字体

    本文发布于:2023-11-28 07:33:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1641489.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:自定义   中以   字体   方式   如何在

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!