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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ TranscriptText(stream: stream)
.imageLoader { image in await myImageCache.cgImage(for: image.source) }
```

### Selection

The text is selectable by default. Turn selection off for text inside a view that reacts to taps, such as a card or a
list row. A tap that is not on a link then goes to that view, and links still open:

```swift
TranscriptText(document)
.selectable(false)
```

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

### Styling

`TranscriptStyle` sets the body size, line, block and list spacing, indents, padding and the maximum image height.
Expand Down
11 changes: 11 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct TranscriptText: UIViewRepresentable {
private var loader: (@Sendable (TranscriptImage) async -> CGImage?)?
private var animatesTags = true
private var animationTiming: TranscriptAnimationTiming = .immediately
private var isSelectable = true
private var playTrigger: AnyHashable?

public init(_ document: TranscriptDocument, style: TranscriptStyle = .init()) {
Expand All @@ -42,6 +43,15 @@ public struct TranscriptText: UIViewRepresentable {
return copy
}

/// Whether the text can be selected; see `TranscriptTextView.isSelectable`.
/// On by default. Turn it off for text inside a view that reacts to taps,
/// such as a card or a list row: taps that are not on a link then reach it.
public func selectable(_ selectable: Bool) -> TranscriptText {
var copy = self
copy.isSelectable = selectable
return copy
}

/// Whether custom tags with an animation play it when they finish
/// arriving; see `TranscriptTextView.animatesTags`. On by default.
public func animatesTags(_ animates: Bool) -> TranscriptText {
Expand Down Expand Up @@ -94,6 +104,7 @@ public struct TranscriptText: UIViewRepresentable {
view.style = style
view.animatesTags = animatesTags
view.tagAnimationTiming = animationTiming
view.isSelectable = isSelectable
if playTrigger != context.coordinator.playTrigger {
context.coordinator.playTrigger = playTrigger
view.playTagAnimations()
Expand Down
25 changes: 24 additions & 1 deletion Sources/TranscriptKit/iOS/TranscriptTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD
/// Called when a link is tapped. By default the URL is opened by the system.
public var openLink: (URL) -> Void = { UIApplication.shared.open($0) }

/// Whether the text can be selected. On by default. Off, the view has no
/// selection gestures or edit menu, and a touch that is not on a link goes
/// to the views behind it, for example a card that reacts to taps. Links
/// still open.
public var isSelectable = true {
didSet {
guard isSelectable != oldValue else { return }
if isSelectable {
addInteraction(textInteraction)
} else {
select(nil)
removeInteraction(textInteraction)
if isFirstResponder { resignFirstResponder() }
}
}
}

/// The text to show. Setting it while a `stream` is attached detaches the stream.
public var document: TranscriptDocument {
get { stream?.document ?? directDocument }
Expand Down Expand Up @@ -820,7 +837,13 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD

// MARK: Responder

public override var canBecomeFirstResponder: Bool { true }
public override var canBecomeFirstResponder: Bool { isSelectable }

/// Without selection, only links take touches.
public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
guard super.point(inside: point, with: event) else { return false }
return isSelectable || linkURL(at: point) != nil
}

/// Read-only: an empty input view keeps the keyboard from appearing.
public override var inputView: UIView? { emptyInputView }
Expand Down