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:.
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView reloadData]; [self updateTableViewForDynamicTypeSize]; } - (void)updateTableViewForDynamicTypeSize { static NSDictionary *cellHeightDictionary; if (!cellHeightDictionary) { cellHeightDictionary = @{ UIContentSizeCategoryExtraSmall : @44, UIContentSizeCategorySmall : @44, UIContentSizeCategoryMedium : @44, UIContentSizeCategoryLarge : @44, UIContentSizeCategoryExtraLarge : @55, UIContentSizeCategoryExtraExtraLarge : @65, UIContentSizeCategoryExtraExtraExtraLarge : @75 }; } NSString *userSize = [[UIApplication sharedApplication] preferredContentSizeCategory]; NSNumber *cellHeight = cellHeightDictionary[userSize]; [self.tableView setRowHeight:cellHeight.floatValue]; [self.tableView reloadData]; }
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.
self.navigationItem.rightBarButtonItem = bbi; self.navigationItem.leftBarButtonItem = [self editButtonItem]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(updateTableViewForDynamicTypeSize) name:UIContentSizeCategoryDidChangeNotification object:nil]; } return self; } - (void)dealloc { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self]; }
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.