I have below code.
let text = "as of 07/29/2020"
textLabel.text = text
Voice Over read it - "as of 07 slash 29 slash 2 Thousand 20"
How can i make it to pronounce like "as of July, 29 2 Thousand 20"
I have below code.
let text = "as of 07/29/2020"
textLabel.text = text
Voice Over read it - "as of 07 slash 29 slash 2 Thousand 20"
How can i make it to pronounce like "as of July, 29 2 Thousand 20"
Answer Not sure exactly what you are trying to solve. But hopefully this helps.
Step 1: You need to use the accessibility label property label.accessibilityLabel = "some string"
Step 2: Convert your date into a more friendly string for voice over. You can do this by using a Date()
object and format it differently for the label and the voice-over label. Here is a link for creating dates in swift.
Below is the link to the documentation and the section that mentions accessibility labels.
Playground Example: Your code will then look something like this.
import UIKit
import PlaygroundSupport
let label = UILabel()
let dateForLabel = formatDate(date: Date(), style: .short)
let dateStringForLabel = "as of \(dateForLabel)"
label.text = dateStringForLabel
let dateForVoiceOverLabel = formatDate(date: Date(), style: .long)
let dateStringForVoiceOver = "as of \(dateForVoiceOverLabel)"
label.accessibilityLabel = dateStringForVoiceOver
func formatDate(date: Date, style: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = style
dateFormatter.timeStyle = .none
let date = date
// US English Locale (en_US)
dateFormatter.locale = Locale(identifier: "en_US")
return dateFormatter.string(from: date) // Jan 2, 2001
}
Link to create a Date() object How do you create a Swift Date object?
Link to apple documentation on Accessibility labels https://developer.apple.com/documentation/uikit/accessibility_for_ios_and_tvos/supporting_voiceover_in_your_app
"Update Your App’s Accessibility For elements that were not accessible to VoiceOver, start by improving their accessibility labels and hints:
The accessibilityLabel
property provides descriptive text that VoiceOver reads when the user selects an element.
The accessibilityHint
property provides additional context (or actions) for the selected element.
Accessibility labels are very important, because they provide the text that VoiceOver reads. A good accessibility label should be short and informative. It is important to note that a UILabel
and an accessibilityLabel
are different things. By default, VoiceOver reads the text associated with standard UIKit controls such as UILabel
and UIButton
. However these controls can also have corresponding accessibilityLabel
properties to add more detail about the label or button..
Depending on the context, hints aren’t always necessary. In some cases, the label provides enough context. If you feel like you’re saying too much in an accessibility label, consider moving that text into a hint.
To ensure that users understand the intent of your interface, you might need to set some accessibility labels manually. Accessibility labels and hints can either be set in Xcode’s Identity Inspector or programmatically."