Colin Humber
Colin Humber

Reputation: 1395

Controlling accessibility voice over readout of numbers in iOS

I'm trying to get an accessibilityValue with a decimal number on a custom UIView to readout as "twenty point one", for example, similar to how voice over reads out the duration and keyframe values on the video trimmer when editing a video in the Photos app.

The default setup reads out the value as "twenty dot one". If I set the accessibilityAttributedLabel instead using the accessibilitySpeechPunctuation key, it reads as "twenty period one".

view.accessibilityAttributedLabel = NSAttributedString(string: "20.1", attributes: [.accessibilitySpeechPunctuation: true])

Without resorting to manually building a numeric string to read out, anyone know how to get the number to read saying "point" instead of "dot" or "period"?

Upvotes: 3

Views: 3078

Answers (1)

Colin Humber
Colin Humber

Reputation: 1395

Got it! Formatting a number using a NumberFormatter with a style of .spellOut will generate a string with the fully spelled out value. Not what we want for a label's text, but exactly what we want for an accessibility label.

let formatter = NumberFormatter()
formatter.numberStyle = .spellOut

let label = UILabel()
label.text = formatter.string(from: 20.1)
label.accessibilityLabel = formatter.string(from: 20.1)

// prints out "twenty point one"
print(label.accessibilityLabel)

Upvotes: 4

Related Questions