在 mac 包中嵌入字体
我有一个正在编写的程序。我想使用一种漂亮的字体。我可以将我的字体嵌入到我的包中并从那里使用它吗?
我的代码...
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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。您应该向您的目标添加一个
复制文件
构建阶段(右键单击您的目标,然后选择添加 > 新构建阶段 > 新复制文件构建阶段)。将复制文件构建阶段的目标设置为资源,路径为
字体
。这将确保字体被复制到应用程序包中名为Fonts
的文件夹中。通过将字体文件拖到构建阶段,将字体文件添加到新的构建阶段。
然后,您需要添加
ATSApplicationFontsPath
键添加到您的Info.plist
文件中,并将包含您的字体的文件夹的名称作为其值:然后您可以在您的通过调用
[NSFont fontWithName:@"yourFontName"]
将应用程序视为内置系统字体。当然,在执行此操作之前,您应该确保您有权分发该字体。
Yes, you can. You should add a
Copy Files
build phase to your target (right-click your target, then choose Add > New Build Phase > New Copy Files Build Phase).Set the destination of the Copy Files build phase to Resources with a path of
Fonts
. This will make sure the font is copied into a folder namedFonts
in your application bundle.Add your font file to the new build phase by dragging the font file onto the build phase.
You then need to add the
ATSApplicationFontsPath
key to yourInfo.plist
file, with the name of the folder containing your font as its value:You can then use the font in your app as if it were a built-in system font by calling
[NSFont fontWithName:@"yourFontName"]
.Of course, you should make sure that you have permission to distribute the font before doing this.
有些人使用碳魔法取得了成功。你应该尝试一下。
就上面的示例而言,
ATSFontActivateFromFileSpecification
在 Leopard 中已被弃用。显然,替换直接使用 FSRef,这更好。Some people had success using Carbon magic. You should try it out.
That being said about the example above,
ATSFontActivateFromFileSpecification
was deprecated in Leopard. Apparently the replacement uses an FSRef directly, which is even better.