Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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

like image 629
Jay Lieske Avatar asked Apr 12 '12 19:04

Jay Lieske


1 Answers

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 }
    }
}

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

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.

like image 131
Ilya Avatar answered Sep 22 '22 23:09

Ilya