forked from lixiang1994/AttributedString
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.swift
342 lines (275 loc) · 11.8 KB
/
Action.swift
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// Action.swift
// ┌─┐ ┌───────┐ ┌───────┐
// │ │ │ ┌─────┘ │ ┌─────┘
// │ │ │ └─────┐ │ └─────┐
// │ │ │ ┌─────┘ │ ┌─────┘
// │ └─────┐│ └─────┐ │ └─────┐
// └───────┘└───────┘ └───────┘
//
// Created by Lee on 2020/6/4.
// Copyright © 2020 LEE. All rights reserved.
//
#if os(macOS)
import AppKit
typealias GestureRecognizer = NSGestureRecognizer
#elseif os(iOS)
import UIKit
typealias GestureRecognizer = UIGestureRecognizer
#elseif os(watchOS)
import WatchKit
typealias GestureRecognizer = WKGestureRecognizer
#elseif os(tvOS)
import TVUIKit
#endif
#if os(iOS) || os(macOS)
extension ASAttributedString {
public struct Action {
/// 触发类型
let trigger: Trigger
/// 高亮属性
let highlights: [Highlight]
/// 触发回调
let callback: (Result) -> Void
/// 内部使用
internal var isExternal: Bool = true
internal var handle: (() -> Void)?
public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) {
self.trigger = trigger
self.highlights = highlights
self.callback = callback
}
internal init(_ trigger: Trigger, _ highlights: [Highlight], _ callback: @escaping (Result) -> Void) {
self.trigger = trigger
self.highlights = highlights
self.callback = callback
self.isExternal = false
}
}
}
extension ASAttributedStringWrapper {
public typealias Action = ASAttributedString.Action
public typealias Highlight = Action.Highlight
}
public extension Array where Element == ASAttributedString.Action.Highlight {
static var defalut: [ASAttributedString.Action.Highlight] = [.foreground(#colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1)), .underline(.single)]
static let empty: [ASAttributedString.Action.Highlight] = []
}
extension ASAttributedString.Action {
public enum Trigger: Hashable {
/// 单击 default
case click
/// 按住
case press
}
public struct Result {
public let range: NSRange
public let content: Content
}
public struct Highlight {
let attributes: [NSAttributedString.Key: Any]
}
}
extension ASAttributedString.Action.Result {
public enum Content {
case string(NSAttributedString)
case attachment(NSTextAttachment)
}
}
extension ASAttributedString.Attribute {
public typealias Action = ASAttributedString.Action
public typealias Trigger = Action.Trigger
public typealias Result = Action.Result
public static func action(_ value: @escaping () -> Void) -> Self {
return action { _ in value() }
}
public static func action(_ value: @escaping (Result) -> Void) -> Self {
return .init(attributes: [.action: Action(with: value)])
}
public static func action(_ trigger: Trigger, _ closure: @escaping () -> Void) -> Self {
return .init(attributes: [.action: Action(trigger, with: { _ in closure() })])
}
public static func action(_ trigger: Trigger, _ closure: @escaping (Result) -> Void) -> Self {
return .init(attributes: [.action: Action(trigger, with: closure)])
}
public static func action(_ highlights: [Action.Highlight], _ closure: @escaping () -> Void) -> Self {
return .init(attributes: [.action: Action(highlights: highlights, with: { _ in closure() })])
}
public static func action(_ highlights: [Action.Highlight], _ closure: @escaping (Result) -> Void) -> Self {
return .init(attributes: [.action: Action(highlights: highlights, with: closure)])
}
public static func action(_ value: Action) -> Self {
return .init(attributes: [.action: value])
}
}
extension ASAttributedString {
public init(_ attachment: ImageAttachment, trigger: Action.Trigger = .click, action: @escaping () -> Void) {
self.value = ASAttributedString(.init(attachment: attachment), .action(trigger, action)).value
}
public init(_ attachment: Attachment, trigger: Action.Trigger = .click, action: @escaping () -> Void) {
self.value = ASAttributedString(.init(attachment: attachment.value), .action(trigger, action)).value
}
public init(_ attachment: ImageAttachment, trigger: Action.Trigger = .click, action: @escaping (Action.Result) -> Void) {
self.value = ASAttributedString(.init(attachment: attachment), .action(trigger, action)).value
}
public init(_ attachment: Attachment, trigger: Action.Trigger = .click, action: @escaping (Action.Result) -> Void) {
self.value = ASAttributedString(.init(attachment: attachment.value), .action(trigger, action)).value
}
public init(_ attachment: ImageAttachment, action: Action) {
self.value = ASAttributedString(.init(attachment: attachment), .action(action)).value
}
public init(_ attachment: Attachment, action: Action) {
self.value = ASAttributedString(.init(attachment: attachment.value), .action(action)).value
}
}
extension ASAttributedStringInterpolation {
public typealias Action = ASAttributedString.Action
public typealias Result = Action.Result
public mutating func appendInterpolation(_ value: ImageAttachment, trigger: Action.Trigger = .click, action: @escaping () -> Void) {
self.value.append(ASAttributedString(.init(attachment: value), .action(trigger, action)).value)
}
public mutating func appendInterpolation(_ value: Attachment, trigger: Action.Trigger = .click, action: @escaping () -> Void) {
self.value.append(ASAttributedString(.init(attachment: value.value), .action(trigger, action)).value)
}
public mutating func appendInterpolation(_ value: ImageAttachment, trigger: Action.Trigger = .click, action: @escaping (Result) -> Void) {
self.value.append(ASAttributedString(.init(attachment: value), .action(trigger, action)).value)
}
public mutating func appendInterpolation(_ value: Attachment, trigger: Action.Trigger = .click, action: @escaping (Result) -> Void) {
self.value.append(ASAttributedString(.init(attachment: value.value), .action(trigger, action)).value)
}
public mutating func appendInterpolation(_ value: ImageAttachment, action: Action) {
self.value.append(ASAttributedString(.init(attachment: value), .action(action)).value)
}
public mutating func appendInterpolation(_ value: Attachment, action: Action) {
self.value.append(ASAttributedString(.init(attachment: value.value), .action(action)).value)
}
}
extension ASAttributedString.Action.Highlight {
public static func foreground(_ value: ASColor) -> Self {
return .init(attributes: [.foregroundColor: value])
}
public static func background(_ value: ASColor) -> Self {
return .init(attributes: [.backgroundColor: value])
}
public static func strikethrough(_ style: NSUnderlineStyle, color: ASColor? = nil) -> Self {
var temp: [NSAttributedString.Key: Any] = [:]
temp[.strikethroughColor] = color
temp[.strikethroughStyle] = style.rawValue
return .init(attributes: temp)
}
public static func underline(_ style: NSUnderlineStyle, color: ASColor? = nil) -> Self {
var temp: [NSAttributedString.Key: Any] = [:]
temp[.underlineColor] = color
temp[.underlineStyle] = style.rawValue
return .init(attributes: temp)
}
public static func shadow(_ value: NSShadow) -> Self {
return .init(attributes: [.shadow: value])
}
public static func stroke(_ width: CGFloat = 0, color: ASColor? = nil) -> Self {
var temp: [NSAttributedString.Key: Any] = [:]
temp[.strokeColor] = color
temp[.strokeWidth] = width
return .init(attributes: temp)
}
}
extension ASAttributedString.Action.Trigger {
func matching(_ gesture: GestureRecognizer) -> Bool {
switch self {
#if os(iOS)
case .click where gesture is UITapGestureRecognizer:
return true
case .press where gesture is UILongPressGestureRecognizer:
return true
#endif
#if os(macOS)
case .click where gesture is NSClickGestureRecognizer:
return true
case .press where gesture is NSPressGestureRecognizer:
return true
#endif
default:
return false
}
}
}
extension NSAttributedString {
func get(_ range: NSRange) -> ASAttributedString.Action.Result {
let substring = attributedSubstring(from: range)
if let attachment = substring.attribute(.attachment, at: 0, effectiveRange: nil) as? NSTextAttachment {
return .init(range: range, content: .attachment(attachment))
} else {
return .init(range: range, content: .string(substring))
}
}
}
extension NSAttributedString.Key {
static let action = NSAttributedString.Key("com.attributed.string.action")
}
extension Array where Element == ASAttributedString.Attribute {
/// 合并Action 当存在多个action时 将所有action合并到一个数组中
/// - Returns: 合并后的数组
func mergedAction() -> Array<Element> {
var temp = self
var actions = temp.compactMap {
$0.attributes[.action] as? ASAttributedString.Attribute.Action
}
actions.append(contentsOf: temp.compactMap {
$0.attributes[.action] as? [ASAttributedString.Attribute.Action]
}.flatMap({ $0 }))
if !actions.isEmpty {
temp.removeAll(where: {
$0.attributes.keys.contains(.action)
})
temp.append(.init(attributes: [.action: actions]))
}
return temp
}
}
#endif
extension NSAttributedString {
func contains(_ name: Key) -> Bool {
var result = false
enumerateAttribute(
name,
in: .init(location: 0, length: length),
options: .longestEffectiveRangeNotRequired
) { (value, range, stop) in
guard value != nil else { return }
result = true
stop.pointee = true
}
return result
}
func get<T>(_ name: Key) -> [NSRange: T] {
var result: [NSRange: T] = [:]
enumerateAttribute(
name,
in: .init(location: 0, length: length),
options: .longestEffectiveRangeNotRequired
) { (value, range, stop) in
guard let value = value as? T else { return }
result[range] = value
}
return result
}
func get(_ range: NSRange) -> [NSRange: [NSAttributedString.Key: Any]] {
var result: [NSRange: [NSAttributedString.Key: Any]] = [:]
enumerateAttributes(in: range, options: .longestEffectiveRangeNotRequired) { (attributes, range, stop) in
result[range] = attributes
}
return result
}
func reset(range: NSRange, attributes handle: (inout [NSAttributedString.Key: Any]) -> Void) -> NSAttributedString {
let string = NSMutableAttributedString(attributedString: self)
enumerateAttributes(
in: range,
options: .longestEffectiveRangeNotRequired
) { (attributes, range, stop) in
var temp = attributes
handle(&temp)
string.setAttributes(temp, range: range)
}
return string
}
}