-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathUITextInteractionView.swift
More file actions
198 lines (167 loc) · 6.4 KB
/
Copy pathUITextInteractionView.swift
File metadata and controls
198 lines (167 loc) · 6.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#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
/// Actions the app adds to the selection menu, after the platform's own.
var selectionActions: [TextSelectionAction]
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,
selectionActions: [TextSelectionAction] = []
) {
self.selectionActions = selectionActions
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 {
let hasSelection = !(model.selectedRange?.isCollapsed ?? true)
switch action {
case #selector(copy(_:)), #selector(share(_:)):
return hasSelection
default:
// Look up, translate, search and the rest are the platform's to offer for selected text; refusing
// every unknown selector was what kept them off the menu.
return hasSelection && super.canPerformAction(action, withSender: sender)
}
}
// The edit menu for a text interaction is assembled through the context menu system, so this is where
// the app's own actions join the platform's without replacing how the selection itself works.
override func buildMenu(with builder: any UIMenuBuilder) {
super.buildMenu(with: builder)
guard builder.system == .context, !selectionActions.isEmpty,
let selectedRange = model.selectedRange, !selectedRange.isCollapsed
else {
return
}
let selectedText = Formatter(model.attributedText(in: selectedRange)).plainText()
let actions = selectionActions.map { action in
UIAction(
title: action.title,
image: action.systemImage.map { UIImage(systemName: $0) } ?? nil
) { _ in
action.handler(selectedText)
}
}
builder.insertChild(
UIMenu(options: .displayInline, children: actions),
atEndOfMenu: .standardEdit
)
}
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)
guard let url = model.url(for: location) else {
model.selectedRange = nil
return
}
openURL(url)
}
@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