Skip to content

Instantly share code, notes, and snippets.

@championofblocks
Created March 27, 2013 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save championofblocks/5256281 to your computer and use it in GitHub Desktop.
Save championofblocks/5256281 to your computer and use it in GitHub Desktop.
UIFont dynamic loading from external resource
+ (void)loadFontAtPath:(NSString*)path{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
if(data == nil){
NSLog(@"Failed to load font. Data at path is null");
return;
}
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(!CTFontManagerRegisterGraphicsFont(font, &error)){
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
for (NSString *f in [UIFont familyNames]) {
NSLog(@"%@", [UIFont fontNamesForFamilyName:f]);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment