Skip to main content

How to set custom accessibility labels and hints

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to set custom accessibility labels and hints 관련

SwiftUI by Example

Back to Home

How to set custom accessibility labels and hints | SwiftUI by Example

How to set custom accessibility labels and hints

Updated for Xcode 16

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
    }
Similar solutions…
All SwiftUI property wrappers explained and compared | SwiftUI by Example

All SwiftUI property wrappers explained and compared
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
Introduction to accessibility with SwiftUI | SwiftUI by Example

Introduction to accessibility with SwiftUI
How to create static labels with a Text view | SwiftUI by Example

How to create static labels with a Text view
How to detect the Reduce Motion accessibility setting | SwiftUI by Example

How to detect the Reduce Motion accessibility setting

이찬희 (MarkiiimarK)
Never Stop Learning.