AccessibilityIdentifier for group of Texts at the same time with identifiers to each of Text

121 Views Asked by At

For example code:

HStack {
    Text("Hello")
        .accessibilityIdentifier("first_word")
    Text(" ")
    Text("World!")
        .accessibilityIdentifier("second_word")
}
// Place, where I want to put "phrase_identifier",
// but it will override "first_word" and "second_word" identifiers

Is it possible to create "phrase_identifier" which return me "Hello World!", and in the same time to have possibility to get text addressing by "first_word" = "Hello", "second_word" = "World!" ?

Update: Below was proposed next solution, but in result it create group of identifiers, and I can't get value or user input label or label by "phrase_identifier"

enter image description here

Next: what shows element enter image description here

1

There are 1 best solutions below

5
Sweeper On BEST ANSWER

If I understand correctly, you should add .accessibilityElement(children: .contain) before the accessibility identifier.

HStack {
    Text("Hello")
        .accessibilityIdentifier("first_word")
    Text(" ")
    Text("World!")
        .accessibilityIdentifier("second_word")
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier("phrase")

See also the code example in the documentation of .contain, which is a similar situation to what you have.

You can also use the .combine behaviour:

.accessibilityElement(children: .combine)

This combines the accessibility properties of the children in some system-defined way.