Home > Software design >  How to control accessibility voiceover text for the SwiftUI Image
How to control accessibility voiceover text for the SwiftUI Image

Time:09-09

For the SwiftUI Image element, the voiceover template is seems "accessibility label - image - image name", e.g. for

var body: some View {
        Image(systemName: "equal")
            .accessibilityLabel("my label")
    }

I am getting voiceover response "my label image equal".

Is it possible for voiceover to only say "my label", and not pronounce the "image equal" part?

CodePudding user response:

Once the element gets the focus, the default trait(link, button, label, etc) will be played after accessibilityLabel text. That's the reason it reads out as "my label -> image"

To add or remove the default trait following methods can be used :

  • .accessibilityAddTraits
  • .accessibilityRemoveTraits

Example

To recognize an image as a button:

Add .isButton trait and remove the .isImage trait, now VoiceOver can read the description of Image as "my label -> button"

struct ContentView: View {
    var body: some View {
        Image(systemName: "equal")
            .accessibilityLabel("my label")
            .accessibilityAddTraits(.isButton)
            .accessibilityRemoveTraits(.isImage)
    }
}

As an element can have multiple traits, remove the ones you don't want the voiceover to read.

  • Related