views:

600

answers:

4

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];
+2  A: 

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.

zneak
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
+5  A: 

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.

Rob Keniger
A: 

Great advice but I ran into a little snag. I don't see where to hit "ok" when adding the new build phase ...

Maybe I am stupid!

RW
Just close the window, it's an inspector. You'll see the new build phase appear under your target. Just drag the font file onto it.
Rob Keniger
Also, comment on answers instead of posting an answer. :)
zneak
Thank you!!!Some of the hardest problems are too obvious.
RW
BTW it works great.
RW
A: 

Hi there! How do you handle the custom font with ATSApplicationFontsPath. I followed all the things said by Rob, but it doesn't seems to work. When i scan the [UIFont familyNames], my custom font doesn't appear in the list.

My question was if i missed something and if there 's a way to check if ATSApplicationFontsPath return something

I forget to mention that i'm workin' for the iphone.

loïc
The above info is for a Mac OS X desktop application. It doesn't apply to iPhone apps. Also, don't use the answer box when a comment or new question would be more suitable.
benzado