I know you can customize fonts by using Interface Builder and selecting a font. However, I'm curious if I can use a custom font that's not included by default on systems. Is there a way to include a custom font in my application?
-
25@poke: This is not a duplicate, this is for **Mac**, as clearly outlined in my tags. Please read more carefully. – sudo rm -rf Mar 12 '11 at 16:01
-
@poke: It's alright. :) – sudo rm -rf Mar 12 '11 at 16:07
-
Guess this would be the better possible duplicate: http://stackoverflow.com/questions/2444717/embed-font-in-a-mac-bundle – can't change my close vote now though :/ – poke Mar 12 '11 at 16:10
-
@poke: That looks perfect. I hadn't seen that even though I searched for it. :) On a side note, would it be even legal for me to try to embed Calibri in my application? – sudo rm -rf Mar 12 '11 at 16:13
-
It's unlikely, being a MS font. You should check the license agreement. – Rob Keniger Mar 12 '11 at 22:39
4 Answers
While the manual font activation procedure is one option, you might also consider the ATSApplicationFontsPath
Info.plist key:
Information Property List Key Reference:
"
ATSApplicationFontsPath
(String
- Mac OS X) identifies the location of a font file or directory of fonts in the bundle’sResources
directory. If present, Mac OS X activates the fonts at the specified path for use by the bundled application. The fonts are activated only for the bundled application and not for the system as a whole. The path itself should be specified as a relative directory of the bundle’s Resources directory. For example, if a directory of fonts was at the path/Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/
, you should specify the stringStuff/MyFonts/
for the value of this key."
I'd be sure to double-check, but I believe this functionality was added in OS X 10.5.x (which the code posted by Jinhyung Park targets).

- 22,699
- 3
- 58
- 66
-
Yeah or you can use Core Text which was introduced in Snow Leopard, I believe. – sudo rm -rf Mar 12 '11 at 21:16
-
This solution is the right way to go. Thanks for pointing it out. – Max Seelemann Sep 15 '11 at 09:45
-
-
Not sure why I didn't mark this as answer before, but there you go. :) – sudo rm -rf Jul 11 '12 at 14:02
-
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
ATSApplicationFontsPath uses [NSBundle mainbundle] path as base path, so it does not work when Resources folder is not located there (e.g. for app plugins).
In my Cocoa plugin I need to load custom fonts using CTFontManagerRegisterFontsForURL
#import <Cocoa/Cocoa.h>
static void FGDActivateFont(NSString *fontName)
{
// Can't make ATSApplicationFontsPath in Info.plist work for plugins (non-standard Resources path)
NSArray *availableFonts = [[NSFontManager sharedFontManager] availableFonts];
if (![availableFonts containsObject:fontName]) {
NSURL *fontURL = [[FGDRapidCart bundle] URLForResource:fontName withExtension:@"ttf" subdirectory:@"Fonts"];
assert(fontURL);
CFErrorRef error = NULL;
if (!CTFontManagerRegisterFontsForURL((__bridge CFURLRef)fontURL, kCTFontManagerScopeProcess, &error))
{
CFShow(error);
}
}
}
int main(int argc, char *argv[])
{
@autoreleasepool
{
FGDActivateFont(@“FontAwesome”);
}
return NSApplicationMain(argc, (const char **)argv);
}
Credits: https://github.com/OneSadCookie/Extendaword/blob/master/Extendaword/main.m

- 420
- 3
- 8
-
Good answer, needed this for my screensaver, plist values wouldn't work there. – Moshe Gottlieb Aug 10 '16 at 07:40
Here is the example for Mac App custom font loading.
NSString *fontFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/fonts"];
fontsURL = [NSURL fileURLWithPath:fontFilePath];
if(fontsURL != nil)
{
OSStatus status;
FSRef fsRef;
CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
status = ATSFontActivateFromFileReference(&fsRef, kATSFontContextLocal, kATSFontFormatUnspecified,
NULL, kATSOptionFlagsDefault, NULL);
if (status != noErr)
{
errorMessage = @"Failed to acivate fonts!";
goto error;
}
}

- 471
- 2
- 10
-
1
-
I read the discussion at http://cocoadev.com/index.pl?UsingCustomFontsInYourCocoaApplications. After loading your fonts, you can use [NSFont fontWithName:@""]. I just checked in my Mac OS X application again that it is working. – Jinhyung Park Mar 12 '11 at 16:24
-
Funny you don't find anything in either Xcode help or ADC when searching for ATSFontActivateFromFileReference - need to go to "ATS/ATSFont.h" on your hard disk for any info.. – Jay Jan 13 '12 at 17:05
-
3ATSFontActivateFromFileReference = Undocumented = App Store Rejection – Tibidabo Sep 23 '13 at 00:47
-
@Tibidabo several years late, but not undocumented at all: https://developer.apple.com/legacy/library/documentation/Carbon/Conceptual/ATS_Concepts/ManagingFonts.pdf – andlabs Jan 21 '16 at 00:11
I have managed to do this using cocos2d (CCLabelBMFont) and hiero tool. You will need to create the font using the hiero, and give this font to the CCLabelBMFont object.

- 2,368
- 1
- 16
- 21