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

How to set custom accessibility labels and hints

Paul Hudson    @twostraws   

Updated for Xcode 16.4

Improved in iOS 18

SwiftUI does a great job of providing sensible default accessibility labels for text, images, and many other built-in views, but very often you'll want to customize this yourself using the accessibilityLabel() and accessibilityHint() modifiers.

Important: Your accessibility labels are read immediately by VoiceOver, and so should be kept short and to the point. Accessibility hints are bonus text that get read after a short delay, and can provide extra information beyond the label.

For example, we could use accessibilityLabel() and accessibilityHint() together to provide better context on what your view means:

Button("Add", systemName: "person.crop.circle") {
    print("Adding friend…)
}
.accessibilityLabel("Add to group")
.accessibilityHint("Add Jess to the Bridgerton Fans chat group.")

If you're able to target iOS 18 and later, there is an additional variant of accessibilityLabel() that is very helpful: we can provide it with a closure to customize whatever is SwiftUI's default label, allowing us to add to an existing description.

For example, the code below will read SwiftUI's default label for the text, but add the word "Warning" before it to reflect the fact that the text is bold and red:

Text("This is an important message")
    .fontWeight(.bold)
    .foregroundStyle(.red)
    .accessibilityLabel { label in
        Text("Warning:")
        label
    }
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!

 
Unknown user

You are not logged in

Log in or create account