Changing the pronunciation in Voice Over

Recently I took on a side project within my main project: improving the accessibility of the app, specifically Voice Over.

One label, however, was giving me trouble. It contained the text "Live", but it was pronouncing it incorrectly.

So I asked around

Other than being obviously hilarious, the tweet got me nowhere - potentially because it made no sense, but probably because people were too busy laughing at my comedy genius.

After a bit of searching I found I wasn't the only one with this problem. But the suggested solution (misspell the word until it's pronounced right) doesn't sit well with me. It results in messy code. Intentional typos? Ugh. Someone is going to clean that up thinking it's a bug, you just know it, and you might not catch it for some time. You might get around that with a comment, but who reads comments #amirite?

No, what I needed was a way to control the pronunciation without having to feel like I needed a bath. Enter a new accessibility attribute introduced in iOS 11: UIAccessibilitySpeechAttributeIPANotation.

There is an attributed counterpart to accessibility labels: accessibilityAttributedLabel which takes an attributed string which can be given certain speech attributes. In the case of UIAccessibilitySpeechAttributeIPANotation you are specifying its pronunciation using the International Phonetic Alphabet or IPA for short. 

Phonetically speaking my app was pronouncing "Live" to rhyme with "Give", but I wanted it to rhyme with "Five". In IPA terms I wanted "laɪv" not "lɪv".

The solution to my problem is very simple

if #available(iOS 11.0, *) {
    let key = NSAttributedStringKey(
        rawValue: UIAccessibilitySpeechAttributeIPANotation
    let attributedString = NSAttributedString(
        string: "Live", attributes: [key: "laɪv"]

    label.accessibilityAttributedLabel = attributedString

And that's it, specify your spelling in the IPA format and Voice Over will do the rest. It's not perfect, it comes out a little truncated, but the code is clear with no potential for confusion over the typos.

One issue persists, and it's not the fault of the IPA, or accessibility in general. But pronunciation issues like this are specific to individual languages. So if you have a multilingual app and you want to be vocal-cord perfect with this solution, it could get out of hand very quickly.

Still, it's great to see such attention to detail from iOS. Pity my app supports iOS 10, though!