forked from gonzalezreal/textual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUITextInteractionView.swift
More file actions
167 lines (137 loc) · 4.97 KB
/
Copy pathUITextInteractionView.swift
File metadata and controls
167 lines (137 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#if TEXTUAL_ENABLE_TEXT_SELECTION && canImport(UIKit)
import SwiftUI
import os
import UniformTypeIdentifiers
// MARK: - Overview
//
// `UITextInteractionView` implements selection and link interaction on iOS-family platforms.
//
// The view sits in an overlay above one or more rendered `Text` fragments. It uses
// `TextSelectionModel` to translate touch locations into URLs and selection ranges, and it
// respects `exclusionRects` so embedded scrollable regions can continue to handle gestures.
// Selection UI is provided by `UITextInteraction` configured for non-editable content.
final class UITextInteractionView: UIView {
override var canBecomeFirstResponder: Bool {
true
}
var model: TextSelectionModel
var exclusionRects: [CGRect]
var openURL: OpenURLAction
weak var inputDelegate: (any UITextInputDelegate)?
let logger = Logger(category: .textInteraction)
private(set) lazy var _tokenizer = UITextInputStringTokenizer(textInput: self)
private let selectionInteraction: UITextInteraction
init(
model: TextSelectionModel,
exclusionRects: [CGRect],
openURL: OpenURLAction
) {
self.model = model
self.exclusionRects = exclusionRects
self.openURL = openURL
self.selectionInteraction = UITextInteraction(for: .nonEditable)
super.init(frame: .zero)
self.backgroundColor = .clear
setUp()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for exclusionRect in exclusionRects {
if exclusionRect.contains(point) {
return false
}
}
return super.point(inside: point, with: event)
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
switch action {
case #selector(copy(_:)), #selector(share(_:)):
return !(model.selectedRange?.isCollapsed ?? true)
default:
return false
}
}
override func copy(_ sender: Any?) {
guard let selectedRange = model.selectedRange else {
return
}
let attributedText = model.attributedText(in: selectedRange)
let formatter = Formatter(attributedText)
UIPasteboard.general.setItems(
[
[
UTType.plainText.identifier: formatter.plainText(),
UTType.html.identifier: formatter.html(),
]
]
)
}
private func setUp() {
model.selectionWillChange = { [weak self] in
guard let self else { return }
self.inputDelegate?.selectionWillChange(self)
}
model.selectionDidChange = { [weak self] in
guard let self else { return }
self.inputDelegate?.selectionDidChange(self)
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
addGestureRecognizer(tapGesture)
selectionInteraction.textInput = self
selectionInteraction.delegate = self
for gesture in selectionInteraction.gesturesForFailureRequirements {
tapGesture.require(toFail: gesture)
}
addInteraction(selectionInteraction)
}
@objc private func handleTap(_ gesture: UITapGestureRecognizer) {
let location = gesture.location(in: self)
if let url = model.url(for: location) {
openURL(url)
} else {
model.selectedRange = nil
}
}
@objc private func share(_ sender: Any?) {
guard let selectedRange = model.selectedRange else {
return
}
let attributedText = model.attributedText(in: selectedRange)
let itemSource = TextActivityItemSource(attributedString: attributedText)
let activityViewController = UIActivityViewController(
activityItems: [itemSource],
applicationActivities: nil
)
if let popover = activityViewController.popoverPresentationController {
let rect =
model.selectionRects(for: selectedRange)
.last?.rect.integral ?? .zero
popover.sourceView = self
popover.sourceRect = rect
}
if let windowScene = window?.windowScene,
let viewController = windowScene.windows.first?.rootViewController
{
viewController.present(activityViewController, animated: true)
}
}
}
extension UITextInteractionView: UITextInteractionDelegate {
func interactionShouldBegin(_ interaction: UITextInteraction, at point: CGPoint) -> Bool {
logger.debug("interactionShouldBegin(at: \(point.logDescription)) -> true")
return true
}
func interactionWillBegin(_ interaction: UITextInteraction) {
logger.debug("interactionWillBegin")
_ = self.becomeFirstResponder()
}
func interactionDidEnd(_ interaction: UITextInteraction) {
logger.debug("interactionDidEnd")
}
}
extension Logger.Textual.Category {
fileprivate static let textInteraction = Self(rawValue: "textInteraction")
}
#endif