SwiftUI’s Label Improves Accessibility
What’s the difference between these two pieces of SwiftUI?
HStack {
Image(systemName: "globe")
Text("Hello, world!")
}
Label("Hello, world!", systemImage: "globe")
In addition to more concise and expressive, the Label
is also better for accessibility. It applies the .accessibilityElement(children: .combine)
modifier to group its subviews.
This allows accessibility tools like VoiceOver and Switch Control to have larger, more semantic hit areas with more meaningful feedback. Here is what gets selected as you go through these subviews with VoiceOver:
View | Image | Text | Label |
---|---|---|---|
Screenshot | |||
Accouncement | Globe, image. Globe. | Hello, world! | Hello, world! |
By using the Label
, you’ve created a better accessibility UX. While you could use the HStack
and the .accessibilityElement(children: .combine)
modifier, using a Label
is simpler and will get you platform native behavior when it’s time to use the code on different platforms, as well.