39

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];
zneak
  • 134,922
  • 42
  • 253
  • 328
RW.
  • 637
  • 6
  • 14
  • 3
    Thank you for posting this question! I was trying to figure out how to do this, and Rob Keniger's answer worked like a charm. You should really mark his answer as the accepted answer. – e.James May 04 '10 at 06:36

2 Answers2

85

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 named Fonts 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 your Info.plist file, with the name of the folder containing your font as its value:

<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"].

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

jtbandes
  • 115,675
  • 35
  • 233
  • 266
Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • 2
    For xCode 5 you have to select your target, the from the menubar select Editor->Add Build Phase->Add Copy Files Build Phase in order to add a build phase. – Kelly Bennett Dec 12 '13 at 20:59
  • Could you please help me for same issue, [my question](http://stackoverflow.com/questions/25279279/unable-to-add-a-custom-font-in-mac-app-osx-10-9-xcode5) – Anoop Vaidya Aug 13 '14 at 06:20
  • @Rob: This works very well. Thanks for that. But what if the user has not installed the font on his mac? Means can we check from the app if the font is installed on mac or not. If not, then can we install that programmatically from cocoa mac application? – Manthan Jul 02 '15 at 12:49
  • The whole point of embedding the font in the app bundle is so that you can load and use it without the user needing to install it. Of course, that means you can only use the font within your app. Checking whether a font is already installed on the system would be more complex. To install it, you could probably just use `NSWorkspace` to open the font file, it will open in the Font Book app which has an Install button. – Rob Keniger Jul 21 '15 at 00:32
2

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.

silverdr
  • 1,978
  • 2
  • 22
  • 27
zneak
  • 134,922
  • 42
  • 253
  • 328
  • 3
    That replacement being `ATSFontActivateFromFileReference`. However, that entire API (http://developer.apple.com/legacy/mac/library/documentation/Carbon/Reference/ATS/) has been deprecated in Snow Leopard, in favor of Core Text. The replacement for the replacement is `CTFontManagerCreateFontDescriptorsFromURL`: http://developer.apple.com/mac/library/documentation/Carbon/Reference/CoreText_FontManager_Ref/Reference/reference.html#//apple_ref/c/func/CTFontManagerCreateFontDescriptorsFromURL That's also easier to use, because Core Text classes are toll-free-bridged with their AppKit counterparts. – Peter Hosey Mar 15 '10 at 04:04