diff --git a/README.md b/README.md index 497eea1..81df825 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Sources/TranscriptKit/iOS/TranscriptText.swift b/Sources/TranscriptKit/iOS/TranscriptText.swift index 3cf57fc..494e049 100644 --- a/Sources/TranscriptKit/iOS/TranscriptText.swift +++ b/Sources/TranscriptKit/iOS/TranscriptText.swift @@ -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()) { @@ -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 { @@ -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() diff --git a/Sources/TranscriptKit/iOS/TranscriptTextView.swift b/Sources/TranscriptKit/iOS/TranscriptTextView.swift index 4dd9d0e..688dc0c 100644 --- a/Sources/TranscriptKit/iOS/TranscriptTextView.swift +++ b/Sources/TranscriptKit/iOS/TranscriptTextView.swift @@ -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 } @@ -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 }