Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptTextView+Accessibility.swift
Original file line number Diff line number Diff line change
@@ -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..<layout.entryCount {
let entry = layout.entry(at: index)
let block = entry.layout.block
let text = (block.text as NSString).substring(to: min(entry.layout.length, block.utf16Count))
if text.isEmpty, block.kind != .rule { continue }
let element = UIAccessibilityElement(accessibilityContainer: self)
element.accessibilityLabel = Self.spokenText(text, of: block)
element.accessibilityFrameInContainerSpace = entry.frame
var traits: UIAccessibilityTraits = .staticText
if case .heading = block.kind { traits.insert(.header) }
var actions: [UIAccessibilityCustomAction] = []
for span in block.spans where span.range.upperBound <= text.utf16.count {
guard let target = span.link ?? span.reference.flatMap({ definitions[$0] }), let url = URL(string: target) else {
continue
}
let title = (text as NSString).substring(with: NSRange(location: span.range.lowerBound, length: span.range.count))
let format = NSLocalizedString("Open %@", comment: "VoiceOver action that opens a link; %@ is the link's text")
actions.append(UIAccessibilityCustomAction(name: String(format: format, title)) { [weak self] _ in
self?.openLink(url)
return true
})
}
if !actions.isEmpty { traits.insert(.link) }
element.accessibilityTraits = traits
element.accessibilityCustomActions = actions
elements.append(element)
}
accessibilityCache = AccessibilityCache(layout: layout, elements: elements)
return elements
}

/// What VoiceOver says for a block: its list marker, then its text.
static func spokenText(_ text: String, of block: TranscriptBlock) -> 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
16 changes: 16 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down