Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to assign an accessibility action to a UILabel?

In our current UI, next to certain labels, we have a help-tip button that when clicked, explains the details of what the label references. As such, VoiceOver identifies these two items as separate accessibility items.

However, when using accessibility, we're hoping we can just do everything in the label itself. This way when the label gets focused, the user will here 'Account value, $20 (the accessibilityLabel), double-tap for help (the accessibilityHint)'

However, unlike a button, a label doesn't have an action associated with it so I'm not sure how to wire up actually triggering the accessibility gesture indicating I want to do something.

Short of converting all of our labels over to buttons, is there any way to listen to the accessibility 'action' method on our labels?

My current work-around is to make only the Help-tip buttons accessible, then move all the relevant information to their accessibility properties, but that seems like code smell as it's easy for a developer to miss that when updating the code.

like image 645
Mark A. Donohoe Avatar asked Sep 06 '16 18:09

Mark A. Donohoe


People also ask

How does swift implement accessibility?

Start by enabling VoiceOver in the Settings app, under General > Accessibility. If you haven't used VoiceOver before, you can scroll around using three-finger swipes, select elements by tapping on them, and activate controls by double tapping. Make sure all elements have accessibility labels.

What does accessibility label do?

Users of accessibility services, such as screen readers, rely on content labels to understand the meaning of elements in an interface. In some cases, such as when information is conveyed graphically within an element, content labels can provide a text description of the meaning or action associated with the element.

What is accessibility identifier in Swift?

An identifier can be used to uniquely identify an element in the scripts you write using the UI Automation interfaces. Using an identifier allows you to avoid inappropriately setting or accessing an element's accessibility label. Current page is accessibilityIdentifier. Apple.

What is accessibility label in iOS?

The label is a very short, localized string that identifies the accessibility element, but does not include the type of the control or view. For example, the label for a Save button is “Save,” not “Save button.” By default, standard UIKit controls and views have labels that derive from their titles.


2 Answers

In your UILabel subclass, override accessibilityActivate() and implement whatever double-tapping should do:

override func accessibilityActivate() -> Bool {
    // do things...
    return true
}

If the action can fail, return false in those instances.

like image 133
Paolo Avatar answered Oct 14 '22 15:10

Paolo


Have your tried adding a UITapGestureRecognizer to the Labels?

Something like :

let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
    tapGesture.numberOfTapsRequired = 1
    sampleLabel.userInteractionEnabled =  true
    sampleLabel.addGestureRecognizer(tapGesture)

func tapResponse(recognizer: UITapGestureRecognizer) {
    print("tap")
}
like image 2
brkr Avatar answered Oct 14 '22 16:10

brkr