O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

15.1. Enumerating and Loading Fonts

Problem

You want to use fonts which come pre-installed on an iOS device, in order to render some text on the screen.

Solution

Use the UIFont class.

Discussion

Fonts are fundamental to displaying text on a graphical user interface. The UIKit framework provides programmers with high-level APIs that facilitate the enumerating, loading, and use of fonts. Fonts are encapsulated in the UIFont class in Cocoa Touch. Each iOS device comes with built-in system fonts. Fonts are organized into families, and each family contains faces. For instance, Helvetica is a font family, and Helvetica Bold is one of the faces of the Helvetica family. To be able to load a font, you must know the font’s face (that is, its name)—and to know the face, you have to know the family. So first, let’s enumerate all the font families that are installed on the device, using the familyNames class method of the UIFont class:

- (void) enumerateFonts{
  
  for (NSString *familyName in [UIFont familyNames]){
    NSLog(@"Font Family = %@", familyName);
  }
  
}

Running this program in iOS Simulator, I get results similar to this:

Font Family = Heiti TC
Font Family = Sinhala Sangam MN
Font Family = Kannada Sangam MN
Font Family = Georgia
Font Family = Heiti J
Font Family = Times New Roman
Font Family = Snell Roundhand
Font Family = Geeza Pro
Font Family = Helvetica Neue
...

After getting the font families, we can enumerate the font names inside each family. We’ll use the fontNamesForFamilyName: class method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required