控制 iOS 中数字的可访问性语音读出

Controlling accessibility voice over readout of numbers in iOS

我正在尝试让自定义 UIView 上带有十进制数字的 accessibilityValue 读出为 "twenty point one",例如,类似于画外音读出持续时间的方式在照片应用中编辑视频时,视频修剪器上的关键帧值。

默认设置读出的值为 "twenty dot one"。如果我设置 accessibilityAttributedLabel 而不是使用 accessibilitySpeechPunctuation 键,它显示为 "twenty period one".

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

无需手动构建要读出的数字字符串,任何人都知道如何让数字读取为 "point" 而不是 "dot" 或 "period"?

知道了!使用样式为 .spellOutNumberFormatter 格式化数字将生成一个具有完整拼写值的字符串。不是我们想要的标签文本,而是我们想要的无障碍标签。

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)