有谁知道如何从 iOS 获取格式化的时间字符串,并且可以通过画外音以自然的方式说出?!点击锁定屏幕上的时钟会说出当前时间的完美本地化版本。很难做到这一点,而且目前似乎无法获得下午 12:45 的以下本地化拼写
时间字符串:
- DE - “12 Uhr 45”
- EN - “12 点”(或 00:45 的“零百四十五分钟”)
- FR - “12 小时 45”
我尝试使用以下代码(仅给出 12:45)以及 DateComponentsFormatter。但没有提供预期的结果。
let formatter = DateFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.timeStyle = .short
formatter.dateStyle = .none
formatter.locale = Locale(identifier: "en_US")
formatter.string(from: Date())
最佳答案
Has anybody an idea how to get formatted time strings from iOS which can be spoken in a natural way by voice over?!
日期和数字必须格式正确才能被 VoiceOver 用户完全理解。
使用下面的元素并查看这些 code snippets (Objc + Swift)找出如何实现完美的读出。
使用您的示例,我建议根据上面链接的代码片段使用以下代码:
let date = dateFormatter.date(from: "06/12/2019 12:45")
let hourComponents = Calendar.current.dateComponents([.hour,],
from: date!)
let mn = Calendar.current.component(.minute, from: date!)
hourLabel.accessibilityLabel = DateComponentsFormatter.localizedString(from: hourComponents,
unitsStyle: .spellOut)! + String(mn)
根据这个原理,您可以拥有一个与您希望 VoiceOver 读出特定时间的方式兼容的格式化字符串。
关于ios - 辅助功能兼容的格式化时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59180099/