Search code examples
iosuilabelvoiceover

Add text to speak before announcing label's text


I have a label in my app that the user should be able to tap on with VoiceOver enabled to have it speak the text of that label. However, I want it to always say something before it reads the label's text. I tried to set the accessibilityLabel via self.displayLabel.accessibilityLabel = "my text here" + self.displayLabel.text! but this results in it always being set to the label's original text value which is always changing in this app.

How can I add text to announce just before the label's content, ensuring it will always speak the label's current text contents?


Solution

  • Override the accessibilityLabel property in the UILabel subclass to return whatever you'd like.

    override var accessibilityLabel: String! {
        get {
            return "my text here" + "," + self.text!
        }
        set { }
    }
    

    You may want to post an announcement each time the text changes to inform VO users the value has changed.