3

When my user presses a button with invalid text i show a UILabel (previously hidden) containing text "Invalid values entered".

I would like voice over to automatically read out this label as it appears. How do i do this?

Royston Yinkore
  • 561
  • 1
  • 8
  • 22

3 Answers3

12

If you want to present some important information to a Voice Over, then you should post an "accessibility announcement":

UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, // announce
                                @"My important information");  // actual text
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • I just threw this behind a button event and nothing worked. Is there something I'm missing? – Royston Yinkore Feb 04 '14 at 16:55
  • Never mind...It seems the button's own accessibility text is interfering. I'd get it to work. Thanks :) – Royston Yinkore Feb 04 '14 at 16:58
  • @RoystonYinkore how did you get it to work? I'm now running it with a delay and that a solution I really do not like... – Saren Inden Jul 30 '19 at 05:29
  • VoiceOver ignores notifications if the user is interacting with the screen. If the user has double tapped the button to perform the action, VoiceOver will still most likely still be reading out the button accessibilityLabel when your notification is posted. User touch/input is treated as a first class citizen and notifications are ignored. I believe your delay is the only options. If you want to queue your VoiceOver notifications (not including user input/touch interaction notifications) you could try using https://github.com/spanage/AccessibilityAnnouncer – Aaron Stephenson Nov 11 '19 at 02:01
2

Swift 5

UIAccessibility.post(notification: .announcement, argument: "your text")

There does not seem to be a queue (still, on iOS 13). If you post your announcement immediately after a user action, such as a button tap, it may get swallowed by the button's automatic voice-over announcement. In this case you should post your announcement after a suitable delay (as mentioned here). If that delay is too short, your announcement might cut into the button's announcement.

AVSpeechUtterance will speak the text to all users, UIAccessibility.post only to those that have selected Voice-Over in their accessibility settings, f.i. because they are visually impaired.

pommy
  • 3,717
  • 1
  • 15
  • 25
  • One can pass an attributed text with the [`accessibilitySpeechQueueAnnouncement `](https://developer.apple.com/documentation/foundation/nsattributedstring/key/2865770-accessibilityspeechqueueannounce) attribute set to `true` to queue the announcement behind existing speech. – David Rönnqvist Jan 21 '21 at 18:33
  • @DavidRönnqvist The attribute does establish a queue among `.announcement` notifications. Unfortunately, focus changes among accessible `UIView`s interrupt this queue, as seems to be the case for the OP. See also Aaron Stephenson's comment to your answer. – pommy Jan 26 '21 at 08:34
-1

You want to do something like this:

    AVSpeechSynthesizer* speechSynth;
    AVSpeechUtterance *utterance;

    speechSynth = [[AVSpeechSynthesizer alloc] init];
    speechSynth.delegate = self;

    utterance = [AVSpeechUtterance speechUtteranceWithString:@"Hello, world!"];
    [speechSynth speakUtterance:utterance];

Note: This is iOS7 only

John Green
  • 504
  • 2
  • 7
  • 1
    Thank you very much. I require the app to work with iOS6 as well. I'd try this out though and upvote it when the code works for me. Thank you – Royston Yinkore Feb 04 '14 at 16:54