Clicky

iOS Dev Nugget 145 Listing Available Fonts

.

Need to run a code review on your codebase? Hire me

I want to share a little snippet of Objective C code I wrote a few years ago that still comes in handy every once in a while:

NSMutableArray* fontNames = [NSMutableArray array];
for (NSString* eachFamilyName in [UIFont familyNames]) {
    for (NSString* eachFontName in [UIFont fontNamesForFamilyName:eachFamilyName]) {
        [fontNames addObject:@[eachFontName, eachFamilyName]];
    }
}
[fontNames sortUsingFunction:fontAndFontFamilyCompare context:nil];
//Sorted list of fontNames

NSInteger fontAndFontFamilyCompare(id first, id second, void* context) {
    return [[first objectAtIndex[0]] compare:[second objectAtIndex[1]]];
}

This generates a list of fonts available. You can easily wrap a UITableView around it and display samples of them. Especially handy when you want to examine what fonts are in a new OS release, or if you are adding your own fonts to your app.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues