如何在UITextField中为占位符文本设置可访问性特征? [英] How can I set an accessibility trait for the placeholder text in UITextField?

查看:96
本文介绍了如何在UITextField中为占位符文本设置可访问性特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过我们的iOS应用程序来解决可访问性问题.该应用程序的功能之一是UITextField,用户可以在其中输入搜索查询.我已将该字段的特征设置为搜索字段",并且VoiceOver大部分时间都在该字段上做得很好.当字段中有文本时,它会读取文本,然后说搜索字段".

I'm going through our iOS app to fix accessibility issues. One of the features of the app is a UITextField in which the user can enter search queries. I've set the trait of the field to be "search field", and VoiceOver does a good job with the field most of the time. When there's text in the field, it reads the text, then says "search field".

我要解决的问题是VoiceOver如何处理占位符文本.当文本字段为空时,我们将设置占位符文本以显示示例查询.由于它显示为灰色文本,因此有视力的用户可以看到它只是占位符文本.但是VoiceOver并没有为视障用户提供这种区分.它只是以与常规文本相同的方式读取占位符文本,而无需额外的描述.

The problem I want to solve is how VoiceOver handles the placeholder text. When the text field is empty, we've set the placeholder text to show a sample query. Since it appears as greyed-out text, sighted users can see that it's just the placeholder text. But VoiceOver doesn't make that distinction for visually impaired users. It just reads the placeholder text the same way as regular text, with no extra description.

是否可以将可访问性特征添加到UITextField的占位符文本?还是有人通过其他方法解决了这个问题?

Is there a way to add an accessibility trait to a UITextField's placeholder text? Or have people worked around this through other means?

推荐答案

从UITextField派生一个自定义类,如下所示(代码在Swift中,但是您可以适应Objective-C):

Derive a custom class from UITextField as follows (code is in Swift but you can adapt to Objective-C):

class MyTextField: UITextField {
    override public var accessibilityValue: String? {
        get { return self.text }
        set { super.accessibilityValue = newValue }
    }
}

使用此类作为自定义类,而不是UITextField.当该字段为空时,这将阻止VoiceOver读取占位符文本.

Use this class as a custom class instead of UITextField. This will stop VoiceOver from reading the placeholder text when the field is empty.

然后将您的辅助功能标签设置为搜索"和可访问性提示将提示您要提示的内容(请参阅Apple的创建提示的指南).您可以通过编程方式分配这些值,尽管最好在Interface Builder中进行分配.

Then set your accessibility label to e.g. "Search" and accessibility hint to whatever you want to hint (see Apple's Guidelines for Creating Hints). You can assign those values programmatically, though it's probably best to assign them in Interface Builder.

这篇关于如何在UITextField中为占位符文本设置可访问性特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆