Skip to content

Instantly share code, notes, and snippets.

@fdstevex
Created September 28, 2013 12:34
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdstevex/6741638 to your computer and use it in GitHub Desktop.
Save fdstevex/6741638 to your computer and use it in GitHub Desktop.
Method to retrieve a list of downloadable fonts on iOS 7. This method may block for a while (it can involve a network call) so don't call on main thread. The return is a dictionary that maps font families to arrays of font names in that family.
+ (NSDictionary *)downloadableFonts
{
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)@{(id)kCTFontDownloadableAttribute : (id)kCFBooleanTrue});
CFArrayRef matchedDescs = CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL);
NSArray *array = (__bridge NSArray *)matchedDescs;
NSMutableDictionary *families = [NSMutableDictionary dictionary];
for (UIFontDescriptor *fontDescriptor in array) {
NSString *familyName = fontDescriptor.fontAttributes[UIFontDescriptorFamilyAttribute];
NSString *displayName = fontDescriptor.fontAttributes[UIFontDescriptorVisibleNameAttribute];
NSString *postscriptName = fontDescriptor.postscriptName;
NSMutableDictionary *family = families[familyName];
if (family == nil) {
family = [NSMutableDictionary dictionary];
family[@"name"] = familyName;
family[@"fonts"] = [NSMutableArray array];
families[familyName] = family;
}
NSMutableDictionary *fontDict = [NSMutableDictionary dictionary];
fontDict[@"displayName"] = displayName;
fontDict[@"postscriptName"] = postscriptName;
fontDict[@"descriptor"] = fontDescriptor;
NSArray *languages = fontDescriptor.fontAttributes[@"NSCTFontDesignLanguagesAttribute"];
fontDict[@"languages"] = [languages componentsJoinedByString:@", "];
NSMutableArray *fonts = family[@"fonts"];
[fonts addObject:fontDict];
}
return families;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment