Skip to content

Instantly share code, notes, and snippets.

@Gurpartap
Created December 19, 2011 07:10
Show Gist options
  • Save Gurpartap/1495827 to your computer and use it in GitHub Desktop.
Save Gurpartap/1495827 to your computer and use it in GitHub Desktop.
Load custom fonts in iOS
#import <dlfcn.h>
NSUInteger loadFonts() {
NSUInteger newFontCount = 0;
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
if (frameworkPath) {
void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
if (graphicsServices) {
BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
if (GSFontAddFromFile)
for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
}
}
return newFontCount;
}
// ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
loadFonts();
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment