I have a textField with a pickerView as the inputView.
Now when I have the voiceover on and select the textField, the voiceover will read this: "Quantity: 3 (content of the textField)", then "textField", then "Double tap to edit".
Is there anyway to make the voiceover just reads the content and skip the following "textField. Double tap to edit"?
I have tried to give the textField another UIAccessibilityTraits/Hints and they are not working.
Thanks!
Assuming your text field is a subclass of UITextField, you're looking for the static text trait.
In Swift
*The example code in this answer was written and tested in Swift 3.
About the
accessibilityTraits
PropertyThe
accessibilityTraits
property is aUInt64
bitmask. UIKit includes named UInt64 constants such asUIAccessibilityTraitStaticText
for ease in remembering which bits represent which settings.When a UITextField is created, its
accessibilityTraits
property is set to "262144" which is "1000000000000000000" in binary. That means the 19th bit from the right signifies "text field". There is not a constant for this setting. I tried but could not figure out how to set the 19th bit to zero. This bit appears protected from editing by the implementation for UITextField. You could subclass UITextField and override the accessibilityTraits property to take full control of it like this...Overriding accessibilityTraits
Using UIAccessibilityTraitStaticText
If the "text field" flag is on or "1" then VoiceOver will announce "text field". As @ChrisCM posted, if the "static text" flag is also on, it cancels out the "Text Field" flag and VoiceOver does not announce anything for the type of control.
The "static text" flag is set by adding decimal "64" doing a bitwise OR of binary "1000000" to the accessibilityTraits property. The
UIAccessibilityTraitStaticText
constant makes this value easy to remember.This code illustrates what is happening:
Adding
UIAccessibilityTraitStaticText
toaccessibilityTraits
in detailConsole Output:
|=
OperatorThe following also works. The
|=
operator takes the existing value and does a bitwise OR with "1000000". Since the original value of theUITextField
accessibilityTraits
is protected, this is not necessary.Assigning a Different Trait
To assign a different trait such as a "button", bitwise OR
UIAccessibilityTraitButton
like this:Console Output:
In this case
UIAccessibilityTraitStaticText
cancels out "text field" whileUIAccessibilityTraitButton
adds "button"