Dynamic Types Tutorial in iOS8 with Swift

With Dynamic Types the user can specify the preferred text size in the Settings app of the device. From now on applications are expected to adjust text to the preferred size. Instead of setting the font size explicitly in your code, you can make use of the preferred font for a given text syle. In this tutorial we will create some labels with some text, which will respond to the preferred text size. This tutorial is built in iOS 8.1 and Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftDynamicTypesTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and drag 6 labels to the main view and center them horizontally. Give each label an unique text. The storyboard should look like this.

hen the size of the font is altered the labels needs to resize too, so we will make use of auto layout. Starting at the top, select the Headline Label, Ctrl + drag to the top of the View. Hold down the Ctrl key and select "Top Space to Top Layout Guide" and "Center Horizontally in Container"

elect the Subheadline Label, Ctrl and drag a little bit next to the Label. Choose "Center Horizontally in Container"

elect the Subheadline Label again and drag to the Headline Label. Select "Vertical Spacing"

Repeat the last two steps for the remaining Labels. When finished, select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Headline Label to the ViewController class  and create the following Outlet.

epeat this step for the remaining Labels, the outlet should look like this in the ViewController class.

@IBOutlet var headlineLabel:UILabel!
@IBOutlet var subheadlineLabel:UILabel!
@IBOutlet var bodyLabel:UILabel!
@IBOutlet var footnoteLabel:UILabel!
@IBOutlet var caption1Label:UILabel!
@IBOutlet var caption2Label:UILabel!

e will create a helper method to change our preffered fonts for our labels. Add the updateFonts method

func updateFonts() {
    headlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
    subheadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
    bodyLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
    footnoteLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote)
    caption1Label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)
    caption2Label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption2)

hese are the six preset styles. When the view appears we will call this helper method. Implement the viewDidAppear method.

override func viewDidAppear(animated: Bool) {

Build and Run, the labels are styled according to the preffered font.

top the Project, go to the Settings App in the iOS Simulator and select General -> Accessibility -> Larger Text. Drag the Slider to the right to increase the preferred font to the largest text.

Build an Run the Project again, The preferred font size of the Labels is now set to the largest font.

You can download the source code of the IOS8SwiftDynamicTypesTutorial at the ioscreator repository on Github.