Created
March 27, 2013 17:25
-
-
Save championofblocks/5256281 to your computer and use it in GitHub Desktop.
UIFont dynamic loading from external resource
This file contains hidden or 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
+ (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