20
Dynamic Type

Creating an interface that appeals to everyone can be daunting. Some people prefer more compact interfaces so they can see more information at a time. Others might want to be able to easily see information at a glance, or perhaps they have poor eyesight. These people have different needs, and good developers strive to make apps that meet those needs.

Dynamic Type is a technology introduced in iOS 7 that helps realize this goal by providing specifically designed text styles that are optimized for legibility. Perhaps more importantly, users can select one of seven different preferred text sizes from within Apple’s Settings application, and apps that support Dynamic Type will have their fonts scaled appropriately. In this chapter, you will update Homepwner to support Dynamic Type. Figure 20.1 shows the application rendered at the smallest and largest user selectable Dynamic Type sizes.

Figure 20.1  Homepwner with Dynamic Type supported

Homepwner with Dynamic Type supported

The Dynamic Type system is centered around text styles. When a font is requested for a given text style, the system will use the user’s preferred text size in association with the text style to return an appropriately configured font. Figure 20.2 shows the six different text styles.

Figure 20.2  Different text styles

Different text styles

Using Preferred Fonts

Implementing Dynamic Type is straightforward. At its most basic level, you get a UIFont for a specific text style and then apply that font to something that displays text, such as a UILabel. Let’s start by updating BNRDetailViewController.

You are going to need to update some attributes of the labels programmatically soon, so add outlets to each of the labels to the class extension in BNRDetailViewController.m.

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​D​e​t​a​i​l​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​(​)​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​s​t​r​o​n​g​)​ ​U​I​P​o​p​o​v​e​r​C​o​n​t​r​o​l​l​e​r​ ​*​i​m​a​g​e​P​i​c​k​e​r​P​o​p​o​v​e​r​;​

@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​T​e​x​t​F​i​e​l​d​ ​*​n​a​m​e​F​i​e​l​d​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​T​e​x​t​F​i​e​l​d​ ​*​s​e​r​i​a​l​N​u​m​b​e​r​F​i​e​l​d​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​T​e​x​t​F​i​e​l​d​ ​*​v​a​l​u​e​F​i​e​l​d​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​d​a​t​e​L​a​b​e​l​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​I​m​a​g​e​V​i​e​w​ ​*​i​m​a​g​e​V​i​e​w​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​T​o​o​l​b​a​r​ ​*​t​o​o​l​b​a​r​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​*​c​a​m​e​r​a​B​u​t​t​o​n​;​

@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​n​a​m​e​L​a​b​e​l​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​s​e​r​i​a​l​N​u​m​b​e​r​L​a​b​e​l​;​
@​p​r​o​p​e​r​t​y​ ​(​w​e​a​k​,​ ​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​v​a​l​u​e​L​a​b​e​l​;​

@​e​n​d​

Now that there is an outlet to each of the labels, add a method that sets the font for each to use the preferred Body style.

-​ ​(​v​o​i​d​)​u​p​d​a​t​e​F​o​n​t​s​
{​
 ​ ​ ​ ​U​I​F​o​n​t​ ​*​f​o​n​t​ ​=​ ​[​U​I​F​o​n​t​ ​p​r​e​f​e​r​r​e​d​F​o​n​t​F​o​r​T​e​x​t​S​t​y​l​e​:​U​I​F​o​n​t​T​e​x​t​S​t​y​l​e​B​o​d​y​]​;​

 ​ ​ ​ ​s​e​l​f​.​n​a​m​e​L​a​b​e​l​.​f​o​n​t​ ​=​ ​f​o​n​t​;​
 ​ ​ ​ ​s​e​l​f​.​s​e​r​i​a​l​N​u​m​b​e​r​L​a​b​e​l​.​f​o​n​t​ ​=​ ​f​o​n​t​;​
 ​ ​ ​ ​s​e​l​f​.​v​a​l​u​e​L​a​b​e​l​.​f​o​n​t​ ​=​ ​f​o​n​t​;​
 ​ ​ ​ ​s​e​l​f​.​d​a​t​e​L​a​b​e​l​.​f​o​n​t​ ​=​ ​f​o​n​t​;​

 ​ ​ ​ ​s​e​l​f​.​n​a​m​e​F​i​e​l​d​.​f​o​n​t​ ​=​ ​f​o​n​t​;​
 ​ ​ ​ ​s​e​l​f​.​s​e​r​i​a​l​N​u​m​b​e​r​F​i​e​l​d​.​f​o​n​t​ ​=​ ​f​o​n​t​;​
 ​ ​ ​ ​s​e​l​f​.​v​a​l​u​e​F​i​e​l​d​.​f​o​n​t​ ​=​ ​f​o​n​t​;​
}​

Now call this method at the end of viewWillAppear: to update the labels before they are visible.

 ​ ​ ​ ​s​e​l​f​.​i​m​a​g​e​V​i​e​w​.​i​m​a​g​e​ ​=​ ​i​m​a​g​e​T​o​D​i​s​p​l​a​y​;​

 ​ ​ ​ ​[​s​e​l​f​ ​u​p​d​a​t​e​F​o​n​t​s​]​;​
}​

The preferredFontForTextStyle: method will return a preconfigured font that is customized for the user’s preferences. Build and run the application, and you will notice that the interface looks largely the same.

Now let’s change the preferred font size. Press the Home button (or use Home from the Hardware menu), and open Apple’s Settings application. Under General, select Text Size, and then drag the slider all the way to the left to set the font size to the smallest value (Figure 20.3).

Figure 20.3  Text size settings

Text size settings

Now, go back into Homepwner. If you return to the BNRDetailViewController, you will notice that the interface has not changed at all! Why is this? Since viewWillAppear: is not called when the application returns from the background, your interface is not getting updated. Luckily, you can be informed when the user changes the preferred font size.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset