Is it possible to assign an accessibility action to a UILabel?
Asked Answered
C

5

6

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.

Clericalism answered 6/9, 2016 at 18:38 Comment(0)
C
8

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.

Corri answered 20/6, 2018 at 2:31 Comment(2)
Interesting. I wasn’t aware of that. Will this work with labels? I would test it, but right now I don’t have access to a Mac.Clericalism
@MarqueIV yes, I just used it on some labels and can confirm it worksCorri
M
2

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")
}
Matteo answered 6/9, 2016 at 22:16 Comment(2)
Ideally, you wouldn't use TapGestureRecognizers because they don't provide multiple action options for accessibility users and are difficult to control when you are using Voice Over. Overriding UIAccessibilityCustomActions is the preferred method.Carburize
This solution that works is only a workaround because the native accessibility tools provide different ways to achieve this goal (accessibilityActivate, accessibilityActivationPoint) : use the appropriate tool in the area it's been designed for. ;o)Lymphangitis
C
0

Ok, this was easier than I thought. To make a UILabel respond to accessibility actions similar to how a button does, you simply implement a UITapGestureRecognizer. The Accessibility framework uses that just like any other UIView.

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(labelTapped))
    testLabel.userInteractionEnabled = true
    testLabel.addGestureRecognizer(tapGestureRecognizer)

Once you do that, your label will respond to accessibility actions.

Clericalism answered 6/9, 2016 at 22:16 Comment(0)
C
0

Absolutely! You can do this by using UIAccessibilityCustomActions on the accessibility element rather than using tap gesture recognizers. This is because accessibility operates differently than normal users and single tapping while the voice over focus lands somewhere will not give you the desired result as in the case of a normal use, nor will it permit you to execute multiple options on the same accessibility element.

At their recent WWDC, Apple put out an excellent video explaining how to add UIAccessibilityCustomActions to any kind of accessibility element. If you start this video 33 minutes in, you will be able to see how this is implemented.

Once in place, your Voice Over users will be able to scroll through the options and select the one that most suits his/her intentions, thereby permitting multiple actions to be accessible from the same UILabel.

Carburize answered 20/6, 2018 at 17:44 Comment(1)
Rather than just using a link, if you can show simple code examples here so you don't have to leave the site, let alone scrub through a video, I'll switch the answer to this since it appears the tap recognizer is not the recommended way.Clericalism
L
0

Group your label and your hint button as one unique accessible element.

Once done, you can use :

According to your environment, I don't recommend to implement a custom action for such a simple use case... the two solutions above should do the job.

Lymphangitis answered 13/3, 2019 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.