Aug 8, 2019
Replicating Apple’s Dynamic Type just like in Apple stock apps.
Download Project
Apple enables user to change the font size of there app and Apple app set a good example in there app such as Music and Contact. You can play around with the setting by going in Setting > Accessibility > Larger Text then flip the switch and drag the bar to maximum. When you go back to Apple Music app, the layout will change.
before and after
We begin by creating a new project. In storyboard we put a nested stackView. Outside stackView contains imageView and a stackView with all the lables. Put constraints accordingly.
We also constraint imageView with aspect ratio and width.
Connect outside stackView to its viewController.
Then we add the following code//// ViewController.swift//import UIKitclass ViewController: UIViewController {@IBOutlet weak var stackView: UIStackView!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.}//1 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {super.traitCollectionDidChange(previousTraitCollection) //2if previousTraitCollection?.preferredContentSizeCategory != traitCollection.preferredContentSizeCategory {let contentSize = traitCollection.preferredContentSizeCategoryif contentSize.isAccessibilityCategory {stackView.axis = .verticalstackView.alignment = .leadingstackView.spacing = 2} else {stackView.axis = .horizontalstackView.alignment = .centerstackView.spacing = 6}}}}
override func traitCollectionDidChange(_previousTraitCollection:UITraitCollection?)
This post is inspired by UseYourLoaf’s blogConfusians
© 2015 - 2025