Skip to content

Instantly share code, notes, and snippets.

@yosshi4486
Created April 14, 2021 05:32
Show Gist options
  • Save yosshi4486/88599ef94c3eac9f425902abcbb67c37 to your computer and use it in GitHub Desktop.
Save yosshi4486/88599ef94c3eac9f425902abcbb67c37 to your computer and use it in GitHub Desktop.
日付表示のアクセシビリティ
private func accessibilityLabelForCreatedDateLabel(from date: Date) -> String {
return String(format: NSLocalizedString("Created: %@", comment: "An accessibility label of created date."), self.accessibilityLabel(for: date) ?? "")
}
private func accessibilityLabelForUpdatedDateLabel(from date: Date) -> String {
return String(format: NSLocalizedString("Updated: %@", comment: "An accessibility label of updated date."), self.accessibilityLabel(for: date) ?? "")
}
private func accessibilityLabel(for date: Date) -> String? {
let timeComponentFormatter = DateComponentsFormatter()
timeComponentFormatter.unitsStyle = .spellOut // spellOutを利用するのが重要な点
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: date)
return timeComponentFormatter.string(from: components)
}
// https://developer.apple.com/documentation/foundation/datecomponentsformatter/unitsstyle
// spellOutは発生用に用意されているフォーマットで、時間をちゃんと読み上げてくれる。
// これが正しい対応かどうかはわからない。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment