我们的应用程序有几个UITextField
,用户可以在其中输入字母数字输入代码,例如“ AA149873A”。
[我们希望旁白将其读为“ AA 1 4 9 8 7 2 A”,但它改为将数字读为数字:“ 149.83、8733”。
是否有某种方法可以配置UITextField
,以便它知道其内容不应被视为数字,而是单个数字?
谢谢。
从iOS 13开始,可以添加字符串属性来指导旁白发声以将字符串拼写为单个字母和数字。
我还没有找到一种方法来引导UITextField
将此属性添加到其内容中。但是,UITextField
子类可以覆盖它的accessibilityValue
以实现此目的。
此处给出的子类添加了一个属性来启用或禁用此行为。
final class AccessibilityTextField: UITextField {
var isAlphanumeric: Bool = false
override public var accessibilityAttributedValue: NSAttributedString? {
get {
guard let text = text, !text.isEmpty else {
return nil
}
return NSAttributedString(string: text, attributes: valueAttributes)
}
set {
// Ignore these values.
_ = newValue
}
}
private var valueAttributes: [NSAttributedString.Key: Any] {
guard #available(iOS 13.0, *), isAlphanumeric else {
return [:]
}
return [.accessibilitySpeechSpellOut: true]
}
}