BLACK FRIDAY: Save 50% on all my books and bundles! >>

How to set heading levels for text

Paul Hudson    @twostraws   

Updated for Xcode 16.4

SwiftUI’s accessibilityHeading() modifier allows us to set a heading level for a piece of text, which makes it much easier to navigate through lots of text for folks using VoiceOver.

There are six heading levels available, from .h1 through to .h6, and each one tells the system how important this heading is so that it can be read or navigated to appropriately. It is not attached to the font size or style, so you should combine the two as appropriate.

Important: You must combine this modifier with .accessibilityAddTraits(.isHeader) in order for VoiceOver to detect your headings.

For example, this code uses both top-level .h1 headings and second-level .h2 headings, along with appropriate fonts:

VStack { Text("Hacking with Swift books") .font(.title) .accessibilityAddTraits(.isHeader) .accessibilityHeading(.h1) .padding(.bottom) Text("Testing Swift") .font(.headline) .accessibilityAddTraits(.isHeader) .accessibilityHeading(.h2) Text("If you're serious about writing great code, you need to be serious about writing great tests. Testing Swift is designed to guide you through unit tests, UI tests, TDD, and much more!") .padding(.bottom) Text("Swift Interview Challenges") .font(.headline) .accessibilityAddTraits(.isHeader) .accessibilityHeading(.h2) Text("Work through real Swift coding problems that help strengthen your skills – perfect for finding and filling holes in your Swift knowledge, and preparing for iOS interviews.") }

That code specifically marks the headings, which means VoiceOver users can skip through to the parts they care about – they might skip through the names of 10 different books before reading the text below.

If you're not familiar with VoiceOver, you can headings out by saying "Enable VoiceOver" to Siri. Once that's done, use two fingers to make a rotation gesture on your screen, which will bring up the VoiceOver rotor – a way of selecting the mode VoiceOver is using.

If you repeat that gesture, you'll move through the default rotor options, which includes being able to read individual characters, individual words, adjust the speaking rate, and so on. One of those options is headings, and once that's activated you can swipe up and down to move through headings, skipping things that aren't headings completely.

Note: When you're done, you can tell Siri to "Disable VoiceOver."

Save 50% in my Black Friday sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Everything but the Code Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Interview Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account