简体   繁体   English

如何在UITextField中为占位符文本设置辅助功能特征?

[英]How can I set an accessibility trait for the placeholder text in UITextField?

I'm going through our iOS app to fix accessibility issues. 我正在浏览我们的iOS应用程序以修复可访问性问题。 One of the features of the app is a UITextField in which the user can enter search queries. 该应用程序的一个功能是UITextField,用户可以在其中输入搜索查询。 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. 我已将该字段的特征设置为“搜索字段”,而VoiceOver在大多数情况下都可以很好地处理该字段。 When there's text in the field, it reads the text, then says "search field". 当字段中有文本时,它会读取文本,然后显示“搜索字段”。

The problem I want to solve is how VoiceOver handles the placeholder text. 我想解决的问题是VoiceOver如何处理占位符文本。 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. 但VoiceOver并没有为视障用户做出这样的区分。 It just reads the placeholder text the same way as regular text, with no extra description. 它只是以与常规文本相同的方式读取占位符文本,没有额外的描述。

Is there a way to add an accessibility trait to a UITextField's placeholder text? 有没有办法为UITextField的占位符文本添加辅助功能特征? Or have people worked around this through other means? 或者让人们通过其他方式解决这个问题?

I believe you can set the accessibilityLabel and other accessibility properties on an NSString object and then use that string as your placeholder text. 我相信您可以在NSString对象上设置accessibilityLabel和其他辅助功能属性,然后将该字符串用作占位符文本。 Voiceover will discover that property and use it. 画外音将发现该财产并使用它。

NSString *placeholderText = @"Search";
placeholderText.accessibilityLabel = @"Try searching for xxxx";
field.placeholder = placeholderText;

Something like that. 这样的事情。 Untested, but I saw it in one of the WWDC developer videos. 未经测试,但我在其中一个WWDC开发者视频中看到过。

WARNING: The behavior in iOS 8.0 and up is not as expected. 警告:iOS 8.0及更高版本中的行为与预期不符。

//In iOS 8+
NSString *placeholderText = @"Search"; //This will be announced
placeholderText.accessibilityLabel = @"Try searching for xxxx";//This will be ignored
field.placeholder = placeholderText;

This answer should be considered outdated. 这个答案应该被认为是过时的。

Derive a custom class from UITextField as follows (code is in Swift but you can adapt to Objective-C): 从UITextField派生自定义类,如下所示(代码在Swift中,但您可以适应Objective-C):

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

Use this class as a custom class instead of UITextField. 将此类用作自定义类而不是UITextField。 This will stop VoiceOver from reading the placeholder text when the field is empty. 当字段为空时,这将阻止VoiceOver读取占位符文本。

Then set your accessibility label to eg "Search" and accessibility hint to whatever you want to hint (see Apple's Guidelines for Creating Hints ). 然后将您的可访问性标签设置为例如“搜索”和可访问性提示,指向您想要提示的任何内容(请参阅Apple的创建提示指南 )。 You can assign those values programmatically, though it's probably best to assign them in Interface Builder. 您可以以编程方式分配这些值,但最好在Interface Builder中分配它们。

You can't. 你不能。 Traits only make sense on Accessibility Elements. 特征仅对辅助功能元素有意义。 For your UITextField to be "static text" is probably not correct. 对于你的UITextField来说,“静态文本”可能不正确。 Setting the trait on the item within the text field will have no effect, even though it may be valid code. 即使可能是有效代码,在文本字段中的项目上设置特征也不起作用。

What you can do, is edit the accessibilityLabel of the control. 您可以做的是编辑控件的accessibilityLabel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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