UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to fix incorrect VoiceOver pronunciations

Swift version: 5.6

Paul Hudson    @twostraws   

As clever as VoiceOver is, sometimes it will get the pronunciation wrong for certain words – particularly when it’s missing some context that would have made clear what the correct pronunciation was.

For example, if you have a UILabel containing the string “Live” should that be pronounced as “liv” or as “lyve”? Or how about “Read” – is that pronounced as “reed” or “red”? There’s no way for VoiceOver to know unless you tell it.

The official way to do this is by using the UIAccessibilitySpeechAttributeIPANotation key in an attributed string, but in practice using that just makes your sounds come out poorly.

An easier way that also produces better results is just to use your own phonetic lettering. For example:

label.text = "read"
label.accessibilityLabel = "red"

Using this approach, the screen will show “read” but VoiceOver users will hear “red” – it works for everyone.

There are two places where this approach either won’t be enough or will prove extremely complicated.

First, if you use foreign languages inside your app they will be read out as if they were the user’s primary language. So, French words might be pronounced as if they were English, for example.

Second, if your app uses punctuation that the user needs to hear audibly, the result won’t be what you hoped for. For example, if you write some Swift code like user.name that will be interpreted by VoiceOver as “user (pause) name” rather than “user period name”.

Both of these problems can be fixed by using special attributes of NSAttributedString. For example, we can specify the language for an attributed string like this:

let attributedString1 = NSAttributedString(
    string: "Bonjour", attributes: [.accessibilitySpeechLanguage: "fr-FR"]
)

label.text = "Bonjour"
label.accessibilityAttributedLabel = attributedString1

And we can tell VoiceOver to read all punctuation like this:

let attributedString2 = NSAttributedString(
    string: "user.name", attributes: [.accessibilitySpeechPunctuation: true]
)

label.text = "user.name"
label.accessibilityAttributedLabel = attributedString2

Much better!

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 11.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.