In project 5 we built Word Scramble, a game where users were given a random eight-letter word and had to produce new words using its letters. This mostly works great with VoiceOver: no parts of the app are inaccessible, although that doesn’t mean we can’t do better.
To see an obvious pain point, try adding a word. You’ll see it slide into the table underneath the prompt, but if you tap into it with VoiceOver you’ll realize it isn’t read well: the letter count is read as “five circle”, and the text is a separate element.
There are a few ways of improving this, but probably the best is to make both those items a single group where the children are ignored by VoiceOver, then add a label for the whole group that contains a much more natural description.
Our current code looks like this:
List(usedWords, id: \.self) { word in
HStack {
Image(systemName: "\(word.count).circle")
Text(word)
}
}
To fix this we need to group the elements inside the HStack
together so we can apply our VoiceOver customization:
List(usedWords, id: \.self) { word in
HStack {
Image(systemName: "\(word.count).circle")
Text(word)
}
.accessibilityElement(children: .ignore)
.accessibilityLabel("\(word), \(word.count) letters")
}
Alternatively, you could break that text up to have a hint as well as a label, like this:
HStack {
Image(systemName: "\(word.count).circle")
Text(word)
}
.accessibilityElement(children: .ignore)
.accessibilityLabel(word)
.accessibilityHint("\(word.count) letters")
Regardless of which you choose, if you try the game again you’ll hear it now reads “spill, five letters”, which is much better.
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.