How to use accessibilityLabel

Generated on 8/6/2024

1 search

To use the accessibilityLabel in SwiftUI, you can follow these guidelines from the session "Catch up on accessibility in SwiftUI":

  1. Combining Labels: If you need to append additional information to an existing label without overriding it, you can use the accessibilityLabel modifier. This modifier now accepts a view and will extract the text from the view to set it on the element's label. This allows you to combine the existing label with new content dynamically. For example, you can append a rating to a trip element's label to make it easier to find with VoiceOver. This is discussed in detail at Catch up on accessibility in SwiftUI (14:54).

  2. Creating Elements for Non-Element Views: If a view does not have an accessibility element by default, such as a shape view, you can create one using the accessibilityLabel modifier. This will make the view visible to accessibility technologies. For instance, you can add a label to describe if a comment is unread by creating an element for the indicator view and setting its label to "unread". This is explained at Catch up on accessibility in SwiftUI (07:06).

  3. Conditional Labels: You can conditionally apply labels using the accessibilityLabel modifier with the isenabled parameter. This allows you to apply the label only when certain conditions are met, and fallback to default labels otherwise. For example, you can apply a "super favorite" label only when a comment is marked as such. This is covered at Catch up on accessibility in SwiftUI (11:06).

For a comprehensive understanding of these features, you can watch the session "Catch up on accessibility in SwiftUI" which covers various aspects of enhancing accessibility in SwiftUI applications.