xcode 在 mac 包中嵌入字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2444717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:11:29  来源:igfitidea点击:

Embed font in a mac bundle

objective-ccocoaxcodefontsnsview

提问by RW.

I have a program I am writing. I want to use a fancy font. Can I just embed my font into my bundle and use it from there.

我有一个正在编写的程序。我想使用花哨的字体。我可以将我的字体嵌入到我的包中并从那里使用它吗?

My code...

我的代码...

NSMutableAttributedString *recOf;
recOf = [[NSMutableAttributedString alloc] initWithString:@"In Recognition of"];
length = [recOf length];
[recOf addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Edwardian Script ITC" size:50] range:NSMakeRange(0, length)];
[[NSColor blackColor] set];
p.x = (bounds.size.width/2)- (([recOf size].width)/2);
p.y = (bounds.size.height/1.7);
[recOf drawAtPoint:p];
[recOf  release];

回答by Rob Keniger

Yes, you can. You should add a Copy Filesbuild phase to your target (right-click your target, then choose Add > New Build Phase > New Copy Files Build Phase).

是的你可以。您应该Copy Files向您的目标添加一个构建阶段(右键单击您的目标,然后选择Add > New Build Phase > New Copy Files Build Phase)。

Set the destination of the Copy Files build phase to Resourceswith a path of Fonts. This will make sure the font is copied into a folder named Fontsin your application bundle.

将 Copy Files 构建阶段的目标设置为Resources,路径为Fonts. 这将确保将字体复制到Fonts应用程序包中命名的文件夹中。

Add your font file to the new build phase by dragging the font file onto the build phase.

通过将字体文件拖到构建阶段,将字体文件添加到新的构建阶段。

You then need to add the ATSApplicationFontsPathkeyto your Info.plistfile, with the name of the folder containing your font as its value:

然后,您需要将ATSApplicationFontsPath密钥添加到您的Info.plist文件中,并将包含您的字体的文件夹的名称作为其值:

<key>ATSApplicationFontsPath</key>
<string>Fonts</string>

You can then use the font in your app as if it were a built-in system font by calling [NSFont fontWithName:@"yourFontName"].

然后,您可以通过调用[NSFont fontWithName:@"yourFontName"].

Of course, you should make sure that you have permission to distribute the font before doing this.

当然,在执行此操作之前,您应该确保您有权分发字体。

回答by zneak

Some people had successusing Carbon magic. You should try it out.

有些人使用碳魔法取得了成功。你应该试试看。

That being said about the example above, ATSFontActivateFromFileSpecificationwas deprecated in Leopard. Apparently the replacement uses an FSRef directly, which is even better.

关于上面的例子,ATSFontActivateFromFileSpecification在 Leopard 中已被弃用。显然,替换直接使用 FSRef,这更好。