diff --git a/README.md b/README.md index e0d2ea1..da3fe0b 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,12 @@ TranscriptText(document) In UIKit, set `isSelectable` on `TranscriptTextView`. +### VoiceOver + +VoiceOver reads the text block by block. Headings are in the rotor's list of headings, list items start with their +number or "Checked" or "Unchecked", and each link in a block is an action that opens it. The spoken words "Open %@", +"Checked", "Unchecked" and "Separator" come from your app's `Localizable.strings`, so you can translate them there. + ### Line limit and alignment For short text in cards and list rows, set a line limit and an alignment. When text is left out, the last line shown diff --git a/Sources/TranscriptKit/iOS/TranscriptTextView+Accessibility.swift b/Sources/TranscriptKit/iOS/TranscriptTextView+Accessibility.swift new file mode 100644 index 0000000..c808c71 --- /dev/null +++ b/Sources/TranscriptKit/iOS/TranscriptTextView+Accessibility.swift @@ -0,0 +1,71 @@ +#if canImport(UIKit) && !os(watchOS) +import UIKit + +/// One accessibility element for each block: VoiceOver reads the text block +/// by block, finds headings with the rotor, and opens links as actions. +extension TranscriptTextView { + /// The elements for `layout`, made again only when the layout changes. + func accessibilityElements(for layout: DocumentLayout) -> [UIAccessibilityElement] { + if let cached = accessibilityCache, cached.layout === layout { return cached.elements } + var elements: [UIAccessibilityElement] = [] + let definitions = layout.document.linkDefinitions + for index in 0.. String { + switch block.marker { + case .task(let checked)?: + let state = checked + ? NSLocalizedString("Checked", comment: "VoiceOver: a task list item that is done") + : NSLocalizedString("Unchecked", comment: "VoiceOver: a task list item that is not done") + return state + ", " + text + case .number(let number)?: + return number + " " + text + case .bullet?, nil: + if case .rule = block.kind { + return NSLocalizedString("Separator", comment: "VoiceOver: a thematic break between blocks") + } + return text + } + } +} + +final class AccessibilityCache { + let layout: DocumentLayout + let elements: [UIAccessibilityElement] + + init(layout: DocumentLayout, elements: [UIAccessibilityElement]) { + self.layout = layout + self.elements = elements + } +} +#endif diff --git a/Sources/TranscriptKit/iOS/TranscriptTextView.swift b/Sources/TranscriptKit/iOS/TranscriptTextView.swift index 06c3880..cd38eab 100644 --- a/Sources/TranscriptKit/iOS/TranscriptTextView.swift +++ b/Sources/TranscriptKit/iOS/TranscriptTextView.swift @@ -879,6 +879,22 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD return CGSize(width: UIView.noIntrinsicMetric, height: ceil(installed.height)) } + // MARK: Accessibility + + /// The elements made for the last layout VoiceOver asked about. + var accessibilityCache: AccessibilityCache? + + /// A container: VoiceOver reads its blocks (see `accessibilityElements(for:)`). + public override var isAccessibilityElement: Bool { + get { false } + set {} + } + + public override var accessibilityElements: [Any]? { + get { accessibilityElements(for: currentLayout) } + set {} + } + // MARK: Responder public override var canBecomeFirstResponder: Bool { isSelectable }