How To Get Available Fonts On iPhone Or iPad Using Blocks

Back in November of 2008 I wrote an example to show how to get a list of all the fonts installed on a device: Available Fonts on the iPhone

In this post I will show you how to get the same information using blocks. The code is similar in length between the two approaches, however, there is something inherently cool about using blocks.

Here is the original (non-block) code:

NSString *family, *font;
for (family in [UIFont familyNames])
  debug(@"\nFamily: %@", family);
  for (font in [UIFont fontNamesForFamilyName:family])
    debug(@"\tFont: %@\n", font);

The output will looks as follows:

Family: Thonburi
Font: Thonburi-Bold
Font: Thonburi

Family: Snell Roundhand
Font: SnellRoundhand-Bold
Font: SnellRoundhand-Black
Font: SnellRoundhand

The same end result using blocks:

// Enumerate the installed font family names
[[UIFont familyNames] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
  debug(@"\nFamily: %@", obj);
  // Enumerate the font names in the each family
  [[UIFont fontNamesForFamilyName:obj] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    debug(@"\tFont: %@\n", obj);

You can read more about the debug macro shown above by going here: Debugging Macros