Determining the User’s Preferred Text Size

Now it is time to turn our attention to the BNRItemsViewController. You will need to update two parts of this view controller for Dynamic Type: the rows of your table view will grow or shrink in response to the user changing the preferred text size, and the BNRItemCell will need to be updated similarly to how you updated the BNRDetailViewController. Let’s begin by updating the table view row height.

The goal is to have the table view row heights reflect the preferred Dynamic Type text size of the user. If the user chooses a larger text size, the rows will be taller in order to accommodate the text. Since this is not a problem that Auto Layout will solve, the row heights will need to be set manually. To do this, you need a way of determining which text size the user has selected.

UIApplication exposes the text size that user selected through its preferredContentSizeCategory property. The method will return a constant NSString with the name of the content size category, which will be one of the following values:

  • UIContentSizeCategoryExtraSmall

  • UIContentSizeCategorySmall

  • UIContentSizeCategoryMedium

  • UIContentSizeCategoryLarge (Default)

  • UIContentSizeCategoryExtraLarge

  • UIContentSizeCategoryExtraExtraLarge

  • UIContentSizeCategoryExtraExtraExtraLarge

Open BNRItemsViewController.m. Create a method that will update the table view row height based on the user-selected text size and call this method in viewWillAppear:.

-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​
{​
 ​ ​ ​ ​[​s​u​p​e​r​ ​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​a​n​i​m​a​t​e​d​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​t​a​b​l​e​V​i​e​w​ ​r​e​l​o​a​d​D​a​t​a​]​;​

 ​ ​ ​ ​[​s​e​l​f​ ​u​p​d​a​t​e​T​a​b​l​e​V​i​e​w​F​o​r​D​y​n​a​m​i​c​T​y​p​e​S​i​z​e​]​;​
}​


-​ ​(​v​o​i​d​)​u​p​d​a​t​e​T​a​b​l​e​V​i​e​w​F​o​r​D​y​n​a​m​i​c​T​y​p​e​S​i​z​e​
{​
 ​ ​ ​ ​s​t​a​t​i​c​ ​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​c​e​l​l​H​e​i​g​h​t​D​i​c​t​i​o​n​a​r​y​;​

 ​ ​ ​ ​i​f​ ​(​!​c​e​l​l​H​e​i​g​h​t​D​i​c​t​i​o​n​a​r​y​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​c​e​l​l​H​e​i​g​h​t​D​i​c​t​i​o​n​a​r​y​ ​=​ ​@​{​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​E​x​t​r​a​S​m​a​l​l​ ​:​ ​@​4​4​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​S​m​a​l​l​ ​:​ ​@​4​4​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​M​e​d​i​u​m​ ​:​ ​@​4​4​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​L​a​r​g​e​ ​:​ ​@​4​4​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​E​x​t​r​a​L​a​r​g​e​ ​:​ ​@​5​5​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​E​x​t​r​a​E​x​t​r​a​L​a​r​g​e​ ​:​ ​@​6​5​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​E​x​t​r​a​E​x​t​r​a​E​x​t​r​a​L​a​r​g​e​ ​:​ ​@​7​5​ ​}​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​u​s​e​r​S​i​z​e​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​[​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​s​h​a​r​e​d​A​p​p​l​i​c​a​t​i​o​n​]​ ​p​r​e​f​e​r​r​e​d​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​]​;​

 ​ ​ ​ ​N​S​N​u​m​b​e​r​ ​*​c​e​l​l​H​e​i​g​h​t​ ​=​ ​c​e​l​l​H​e​i​g​h​t​D​i​c​t​i​o​n​a​r​y​[​u​s​e​r​S​i​z​e​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​t​a​b​l​e​V​i​e​w​ ​s​e​t​R​o​w​H​e​i​g​h​t​:​c​e​l​l​H​e​i​g​h​t​.​f​l​o​a​t​V​a​l​u​e​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​t​a​b​l​e​V​i​e​w​ ​r​e​l​o​a​d​D​a​t​a​]​;​
}​

Build and run the application. If you change the Dynamic Type preferred text size and restart the application again, you should notice that the table view row heights reflects the user’s selected text size. (If you don’t restart the application from Xcode, you’ll need to go to the BNRDetailViewController and then return to the BNRItemsViewController.)

Just as you did with the BNRDetailViewController earlier, you need to have the BNRItemsViewController register itself as an observer for the UIContentSizeCategoryDidChangeNotification.

In BNRItemsViewController.m, register for the notification in init, and remove the view controller as an observer in dealloc. Finally, implement the notification call back to call the updateTableViewForDynamicTypeSize method that you just created.

 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​I​t​e​m​.​r​i​g​h​t​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​=​ ​b​b​i​;​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​I​t​e​m​.​l​e​f​t​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​=​ ​[​s​e​l​f​ ​e​d​i​t​B​u​t​t​o​n​I​t​e​m​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​*​n​c​ ​=​ ​[​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​d​e​f​a​u​l​t​C​e​n​t​e​r​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​n​c​ ​a​d​d​O​b​s​e​r​v​e​r​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​e​l​e​c​t​o​r​:​@​s​e​l​e​c​t​o​r​(​u​p​d​a​t​e​T​a​b​l​e​V​i​e​w​F​o​r​D​y​n​a​m​i​c​T​y​p​e​S​i​z​e​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​n​a​m​e​:​U​I​C​o​n​t​e​n​t​S​i​z​e​C​a​t​e​g​o​r​y​D​i​d​C​h​a​n​g​e​N​o​t​i​f​i​c​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​b​j​e​c​t​:​n​i​l​]​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

-​ ​(​v​o​i​d​)​d​e​a​l​l​o​c​
{​
 ​ ​ ​ ​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​*​n​c​ ​=​ ​[​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​d​e​f​a​u​l​t​C​e​n​t​e​r​]​;​
 ​ ​ ​ ​[​n​c​ ​r​e​m​o​v​e​O​b​s​e​r​v​e​r​:​s​e​l​f​]​;​
}​

The table view row height should update now as you change the preferred Dynamic Type text size. Build and run the application and test this out.

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

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