O'Reilly logo

iOS 7 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

20.1. Enumerating and Loading Fonts

Problem

You want to use fonts that come preinstalled 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 = Thonburi
Font Family = Academy Engraved LE
Font Family = Snell Roundhand
Font Family = Avenir
Font Family = Marker Felt
Font Family = Geeza Pro
Font Family = Arial Rounded MT Bo
Font Family = Trebuchet MS
...

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

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