Store Search : Dynamic Type Exercise

Hi there,

I worked through the exercise for dynamic type to scale after app comes back into foreground after changing the dynamic type size. Did the following in AppDelegate.swift :

    func applicationWillEnterForeground(_ application: UIApplication) {
                    // Dynamic type update table view
        let searchViewController = window!.rootViewController as! SearchViewController
        searchViewController.tableView.reloadData()
        
        // Dynamic Type update DetailView that might be open
        if let detailViewController = searchViewController.presentedViewController as? DetailViewController {
            detailViewController.updateUI()
        }
    }

Pretty straight forward I guess ? I added the update of the detail view as well but to really make it work I had to call

someLabel.font = UIFont.preferredFont(forTextStyle: …)

for every label to make it work, without that the view would not update automatically.

Is there something I am missing here ? This does not seem to be scalable with adding more and more screens you would have to take care of whether that view is currently presented and call the according method.

Cheers,
Mark

As of iOS 10 there is a new property adjustsFontForContentSizeCategory on UILabel. If you set this to true, then the app will automatically change this label whenever the font size changes. That should save you some time. :smiley:

1 Like

Thank you :wink: Will give it a go for sure !! Cheers, Mark

Hello! Sorry I was late to the party. So, I’ve done the Dynamic Type exercise by fixing the Auto Layout and replacing this code in viewDidLoad():
tableView.rowHeight = 80

into:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100

Then I test the app in my iPhone without even bothering to create the observer first. Then I was curious to change the text size, and suddenly the app font size changed as well.

I am curious why this could happen as I haven’t add any notifications observer yet.
Is it related to this new property you mentioned above?

Thank you in advance!

Best,

EDIT: after some checking, the property adjustFontForContentSizeCategory is set default as true.
That’s why I don’t have to put any observer as the book asks me to do.

Thanks, for good advice. This property works.