Saturday, January 5, 2013

Listing the Fonts Available in iOS

iOS 5.0 was a huge update in many ways and one of them is built-in font selection. For the first time ever, iPhone and iPad have exactly the same fonts! Many times, we need to know the list of available fonts in iOS, sometimes to display to end-user so that he can write any text in custom font or sometimes to know ourselves the available fonts which we can use. Since Apple is always adding to the available fonts with each iOS release, the best way to know what fonts you can use is to query the iOS for available fonts under the UIFont class.Here is an excellent bit of compact code to print out the available fonts to your console.

NSMutableArray *fontNames = [[NSMutableArray alloc] init];
NSArray *fontFamilyNames = [UIFont familyNames];
for (NSString *familyName in fontFamilyNames) {
    NSLog(@"Font Family Name = %@", familyName);
    NSArray *names = [UIFont fontNamesForFamilyName:familyName];
    NSLog(@"Font Names = %@", fontNames);
    [fontNames addObjectsFromArray:names];

Fonts are part of a font family, and the UIFont class returns a list of all of the font families available in iOS as well as the fonts under each font family. The code above first gets a list of all the font families, and then a list of all the fonts under each family, printing those font names out to the console.