12

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?

Jay Lieske
  • 4,788
  • 3
  • 30
  • 41

3 Answers3

3

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.

Ilya
  • 5,533
  • 2
  • 29
  • 57
2

I believe you can set the accessibilityLabel and other accessibility properties on an NSString object and then use that string as your placeholder text. 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.

WARNING: The behavior in iOS 8.0 and up is not as expected.

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

MobA11y
  • 18,425
  • 3
  • 49
  • 76
Andrew Tetlaw
  • 2,669
  • 22
  • 27
  • This is non-sensical. Traits only apply to UI Objects. – MobA11y Aug 13 '15 at 00:07
  • 1
    That is referencing segmented controls and accessibility LABELS not traits. Please see this open source repository to see why your solution is in fact, non-sensical, and also why traits do not work on NSString objects, EVEN when they are applied to segmented controls. Segmented controls DO happen to be an exception for accessibilityLabels on NSStrings, though I did not comment on these. However, in my demo you will see that this exception DOES NOT apply to placeholder text, as is required by the OP. https://github.com/chriscm2006/iOS-Traits-on-NSStrings – MobA11y Aug 15 '15 at 01:54
  • The OP asked "Or have people worked around this through other means?" hence my suggestion. See also http://adcdownload.apple.com/wwdc_2012/wwdc_2012_session_pdfs/session_210__accessibility_for_ios.pdf they also use the label on table view index titles. That's from 2012, when I wrote the original answer. Besides any object can use UIAccessibility, you can make anything an accessibility element. – Andrew Tetlaw Aug 15 '15 at 02:14
  • 1
    Yes, you can implement protocol for nsstrings and other objects, but as my demo clearly points out, that information can be (and is) ignored. Lots of things that are valid code do not work. If this behavior was valid in ios 6/7, it is not now, so your work around is invalid. – MobA11y Aug 15 '15 at 02:20
  • No worries. Perhaps leaving the legacy answer is reasonable, as long as people know that the code, in ios 8, does not work as implied in your post. I may post a corresponding edit when not on my mobile device :) – MobA11y Aug 15 '15 at 02:34
1

You can't. Traits only make sense on Accessibility Elements. For your UITextField to be "static text" is probably not correct. 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.

MobA11y
  • 18,425
  • 3
  • 49
  • 76