回答
不确定您要解决的确切问题。但希望这会有所帮助。
第一步:需要使用无障碍标签属性label.accessibilityLabel = "some string"
第 2 步:将您的日期转换为更友好的语音字符串。您可以通过使用Date() 对象来执行此操作,并为标签和画外音标签设置不同的格式。这是一个快速创建日期的链接。
以下是文档的链接以及提及可访问性标签的部分。
Playground 示例:您的代码将如下所示。
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
}
创建 Date() 对象的链接
How do you create a Swift Date object?
关于无障碍标签的苹果文档链接
https://developer.apple.com/documentation/uikit/accessibility_for_ios_and_tvos/supporting_voiceover_in_your_app
“更新您应用的辅助功能
对于 VoiceOver 无法访问的元素,首先要改进其可访问性标签和提示:
accessibilityLabel 属性提供了用户选择元素时 VoiceOver 读取的描述性文本。
accessibilityHint 属性为所选元素提供额外的上下文(或操作)。
辅助功能标签非常重要,因为它们提供 VoiceOver 读取的文本。一个好的可访问性标签应该简短且内容丰富。重要的是要注意 UILabel 和 accessibilityLabel 是不同的东西。默认情况下,VoiceOver 会读取与标准 UIKit 控件相关的文本,例如 UILabel 和 UIButton。但是,这些控件也可以具有相应的 accessibilityLabel 属性,以添加有关标签或按钮的更多详细信息..
根据上下文,提示并不总是必要的。在某些情况下,标签提供了足够的上下文。如果您觉得自己在辅助功能标签中说得太多,请考虑将该文本移到提示中。
为确保用户了解您界面的意图,您可能需要手动设置一些可访问性标签。可访问性标签和提示可以在 Xcode 的 Identity Inspector 中设置,也可以通过编程方式设置。”