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*familyNamein[UIFontfamilyNames]){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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access