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 @@ -262,6 +262,9 @@ TranscriptText(document)

In UIKit, set `isSelectable` on `TranscriptTextView`.

On devices that have Writing Tools, the edit menu can offer them for the selected text, as in a `UITextView`. Turn
them off with `.showsWritingTools(false)`, or with `showsWritingTools` on `TranscriptTextView`.

### VoiceOver

VoiceOver reads the text block by block. Headings are in the rotor's list of headings, list items start with their
Expand Down Expand Up @@ -332,6 +335,9 @@ TranscriptText(document)
}
```

One view in a window has a selection at a time. When the person selects text in another view, the first view's
selection ends, and its range is nil. The selection stays when the person opens Share or Translate from the edit menu.

In UIKit, use `onSelectionChange`, `selectedRange` and `selectsAllWhenShown` on `TranscriptTextView`.

## In this repository
Expand Down
19 changes: 19 additions & 0 deletions Sources/TranscriptKit/Layout/DocumentLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public final class DocumentLayout: @unchecked Sendable {
public let length: Int
/// How many block layouts were carried over from the previous layout.
public let reusedBlockCount: Int
/// The lowest point that the glyphs of the text draw. It can be below
/// `height`: marks stacked under a letter draw past the line's descent.
/// It is measured only for a layout shorter than one tile (see
/// `TileGrid`). For a taller layout, it is `height`.
let glyphBottom: CGFloat

/// One entry per settled block of `document`. Their positions never
/// change, so successive layouts share this list and only append to it.
Expand Down Expand Up @@ -132,6 +137,20 @@ public final class DocumentLayout: @unchecked Sendable {
self.height = y
self.length = max(start - 1, 0)
self.reusedBlockCount = reused
self.glyphBottom = y < TileGrid.height ? Self.glyphBottom(of: Array(settled) + open) : y
}

private static func glyphBottom(of entries: [Entry]) -> CGFloat {
var bottom: CGFloat = 0
for entry in entries {
for line in entry.layout.lines {
let glyphs = CTLineGetBoundsWithOptions(line.ctLine, .useGlyphPathBounds)
guard !glyphs.isNull, !glyphs.isEmpty else { continue }
// Core Text measures up from the baseline.
bottom = max(bottom, entry.y + line.baseline - glyphs.minY)
}
}
return bottom
}

/// The next item of a list sits closer to the list content above it than
Expand Down
26 changes: 26 additions & 0 deletions Sources/TranscriptKit/Layout/TileGrid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import CoreGraphics

/// How `TranscriptTextView` cuts its text into the tiles that it draws.
enum TileGrid {
/// The height of a tile in a view that is taller than one tile.
static let height: CGFloat = 256

/// The height of the smallest tile.
static let minimumHeight: CGFloat = 32

/// The height of tile `index` in a view `viewHeight` points high, whose
/// glyphs draw down to `glyphBottom`. A view shorter than one tile has one
/// tile that covers only the view, the shapes that reach below its last
/// line and the glyphs, rounded up to 32, 64, 128 or 256 points. So a
/// one-line text keeps a small drawing, and text that grows resizes its
/// tile at most three times. In a taller view, every tile is 256 points
/// high, so a long answer that streams in does not resize each new tile
/// as it grows.
static func tileHeight(at index: Int, viewHeight: CGFloat, glyphBottom: CGFloat = 0) -> CGFloat {
let needed = max(viewHeight + TranscriptPath.maximumOverhang, glyphBottom)
guard index == 0, needed < height else { return height }
var tile = height
while tile > minimumHeight, tile / 2 >= needed { tile /= 2 }
return tile
}
}
10 changes: 10 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct TranscriptText: UIViewRepresentable {
private var playTrigger: AnyHashable?
private var selectionChange: ((Range<Int>?) -> Void)?
private var selectsAll = false
private var showsWritingTools = true
/// Changed when the view's height changes before SwiftUI has placed it,
/// so that SwiftUI measures the view again (see
/// `TranscriptTextView.onHeightChangeWhileUnsized`).
Expand Down Expand Up @@ -133,6 +134,14 @@ public struct TranscriptText: UIViewRepresentable {
return copy
}

/// Whether the edit menu offers Writing Tools; see
/// `TranscriptTextView.showsWritingTools`. On by default.
public func showsWritingTools(_ shows: Bool) -> TranscriptText {
var copy = self
copy.showsWritingTools = shows
return copy
}

public final class Coordinator {
var playTrigger: AnyHashable?
}
Expand Down Expand Up @@ -168,6 +177,7 @@ public struct TranscriptText: UIViewRepresentable {
view.textFadeInDuration = textFadeInDuration
view.tagAnimationTiming = animationTiming
view.isSelectable = isSelectable
view.showsWritingTools = showsWritingTools
if playTrigger != context.coordinator.playTrigger {
context.coordinator.playTrigger = playTrigger
view.playTagAnimations()
Expand Down
Loading
Loading