Created
December 19, 2011 07:10
-
-
Save Gurpartap/1495827 to your computer and use it in GitHub Desktop.
Load custom fonts in iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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