我正在开发一个应用程序,我需要有标签显示时间,本地化为用户的语言环境.这一切都可以通过使用NSDateFormatter()类来正常工作.
let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = NSDateFormatterStyle.NoStyle
timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let timeStr: String = timeFormatter.stringFromDate(time)
Run Code Online (Sandbox Code Playgroud)
但NSDateFormatter()VoiceOver没有正确说出生成的字符串.这对美国英语来说还不错,但不是荷兰语(我的母语).
然后我尝试使用NSDateComponentsFormatter()该类并NSDateComponentsFormatterUnitsStyle.SpellOut为该unitsStyle属性指定.
let timeComponentFormatter = NSDateComponentsFormatter()
timeComponentFormatter.unitsStyle = NSDateComponentsFormatterUnitsStyle.SpellOut
let accessibilityTimeStr: String = timeComponentFormatter.stringFromDateComponents(timeComponents)!
Run Code Online (Sandbox Code Playgroud)
虽然这会产生一个可以由VoiceOver说出的好字符串,但它并不像母语人士那样说时间.虽然它使用了正确的语言,但它并没有按照用户所在地区的文化惯例进行格式化.
当我在启用了VoiceOver的情况下点击锁定屏幕上的时间时,它会正确地说出时间,就像母语人士所说的那样,用我尝试过的每种语言.因此,iOS中必须有能够生成可由VoiceOver说出的正确字符串的内容.
我找不到这样做的公共API.我错过了什么,或者这可能是私有API?
| 归档时间: |
|
| 查看次数: |
535 次 |
| 最近记录: |