简体   繁体   中英

Voiceover - Overriding a UILabel's accessibilityLabel app-wide

I work on an education app for teachers. In the US, we have a pseudo-grade named Pre-Kindergarten (sometimes still called Preschool). It is commonly written as "PreK" with a pronunciation of "PreeKay". This app displays grade names on many screens. The data source is "PreK" for this grade, which is the correct text to display.

VoiceOver reads "PreK" as if it were a word, "prek". While I'm sure VoiceOver users will know what this means, I'd like to get it right.

I know I can set the accessibilityLabel to "PreeKay" on an individual basis, and that works. I would like to make this work app-wide, as cleanly as possible.

The option I'm considering is a custom UILabel to use whenever grade names are displayed, with code like this:

class GradeLabel: UILabel {
    var _text: String?
    override var text: String? {
        get { _text }
        set { _text = newValue
            super.text = _text
            guard let thisText = _text?.uppercased() else { return }
            // The same label is sometimes used for other text, so I don't want to replace all
            //  occurrences of "PREK" should that be a legitimate substring
            //  NLTokenizer is probably a more correct approach for this.
            if thisText == "PREK" || thisText.starts(with: "PREK ") || thisText.contains(" PREK ") {
                accessibilityLabel = thisText.replacingOccurrences(of: "PREK", with: "PREEKAY")
            }
        }
    }
}

I'm not aware of such a thing, but I'm wondering if there's an app-wide "automatic VoiceOver translator" for custom words, along the lines of the built-in localization tools.

If such an "automatic VoiceOver translator" doesn't exist, I'm looking for feedback on my approach above. Overriding drawRect feels heavy-handed to me - but was the only "drop in replacement" method I came up with. Thank you to @matt for inspiring something better than drawRect!

What you're proposing to do is a really bad idea. Don't read the presentation of the label (its text ) and misuse it as data . The time to set this information is the time when you originally set the text of the label in the first place. You must be doing that somewhere; find that code and modify it so that at the same time you set the text to "PreK" you also set the accessibility label to "PreeKay".

(We have a very similar situation in our app; we often use a label "Qty" that must be read aloud as "Quantity". So every place in the app that does set a label's text to "Qty" must also set its accessibility label to "Quantity". When we realized that we had this issue, it only took a few minutes to find all the places where where we were doing this, and fix them.)

Keep in mind that if you change the "accessible name", whether through accessibilityLabel or another property, that not only does that affect what VoiceOver says (which is what you want) but it also affects Braille devices (which is what you don't want). A Braille user would read "PreeKay". They would never see "PreK". That might be ok but could be confusing.

It's unfortunate that VoiceOver doesn't behave like other screen readers such as NVDA or JAWS that will honor the case of letters and treat a camel or mixed case word as multiple works. That is, NVDA and JAWS will already say "PreK" as "PreeKay" because it sees the "Pre" separate from the "K" because they're uppercased.

While this isn't an ideal solution because every user would have to do this, VoiceOver users have control over how VoiceOver announces things. They can go into Settings > Accessibility > VoiceOver > Speech > Pronunciations and add any words/phrases they want and give phonetic pronunciations, either by spelling the word how they want it pronounced or recording how they want it announced (which in turn translates it into a phonetic pronunciation where you can choose which one sounds closest to what you want, so it still uses VoiceOver's voice and not your voice.)

画外音发音设置截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM