Search code examples
iosswiftuilabelaccessibilityvoiceover

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.


Solution

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