简体   繁体   中英

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"?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM