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 ...
Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.