Skip to content

Commit 5c72b28

Browse files
committed
Checking 增加 case attachment 附件类型, UILabel / UITextView / NSTextField func observe() 回调增加NSRange.
1 parent bbc0d18 commit 5c72b28

5 files changed

Lines changed: 104 additions & 29 deletions

File tree

AttributedString.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AttributedString"
4-
s.version = "1.6.0"
4+
s.version = "1.6.1"
55
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"
66

77
s.homepage = "https://github.com/lixiang1994/AttributedString"

Sources/Checking.swift

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ extension AttributedString {
2525
/// 正则表达式
2626
case regex(String)
2727
#if os(iOS) || os(macOS)
28+
/// 动作
2829
case action
2930
#endif
30-
///
31+
#if !os(watchOS)
32+
/// 附件
33+
case attachment
34+
#endif
3135
case date
3236
case link
3337
case address
@@ -44,9 +48,11 @@ extension AttributedString.Checking {
4448
/// 正则表达式
4549
case regex(NSAttributedString)
4650
#if os(iOS) || os(macOS)
47-
case action(AttributedString.Action.Result)
51+
case action(AttributedString.Action.Result.Content)
52+
#endif
53+
#if !os(watchOS)
54+
case attachment(NSTextAttachment)
4855
#endif
49-
5056
case date(Date)
5157
case link(URL)
5258
case address(Address)
@@ -167,11 +173,34 @@ extension AttributedString {
167173
case .action:
168174
let actions: [NSRange: AttributedString.Action] = value.get(.action)
169175
for action in actions where !contains(action.key) {
170-
result[action.key] = (.action, .action(value.get(action.key)))
176+
result[action.key] = (.action, .action(value.get(action.key).content))
177+
}
178+
#endif
179+
180+
#if !os(watchOS)
181+
case .attachment:
182+
let attachments: [NSRange: NSTextAttachment] = value.get(.attachment)
183+
func allow(_ range: NSRange, _ attachment: NSTextAttachment) -> Bool {
184+
#if os(iOS)
185+
return !contains(range) && !(attachment is ViewAttachment)
186+
#else
187+
return !contains(range)
188+
#endif
189+
}
190+
for attachment in attachments where allow(attachment.key, attachment.value) {
191+
result[attachment.key] = (.attachment, .attachment(attachment.value))
171192
}
172193
#endif
173194

174-
case .date, .link, .address, .phoneNumber, .transitInformation:
195+
case .link:
196+
// 优先获取Link属性的值
197+
let links: [NSRange: URL] = value.get(.link)
198+
for link in links where !contains(link.key) {
199+
result[link.key] = (.link, .link(link.value))
200+
}
201+
fallthrough
202+
203+
case .date, .address, .phoneNumber, .transitInformation:
175204
guard let detector = try? NSDataDetector(types: NSTextCheckingAllTypes) else { return }
176205

177206
let matches = detector.matches(

Sources/Extension/AppKit/NSTextFieldExtension.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,33 @@ extension AttributedStringWrapper where Base: NSTextField {
6767
/// - checking: 检查类型
6868
/// - highlights: 高亮样式
6969
/// - callback: 触发回调
70-
public func observe(_ checking: Checking, highlights: [Highlight] = .defalut, with callback: @escaping (Checking.Result) -> Void) {
70+
public func observe(_ checking: Checking,
71+
highlights: [Highlight] = .defalut,
72+
with callback: @escaping (Checking.Result) -> Void) {
7173
observe([checking], highlights: highlights, with: callback)
7274
}
73-
7475
/// 添加监听
7576
/// - Parameters:
7677
/// - checkings: 检查类型
7778
/// - highlights: 高亮样式
7879
/// - callback: 触发回调
79-
public func observe(_ checkings: [Checking] = .defalut, highlights: [Highlight] = .defalut, with callback: @escaping (Checking.Result) -> Void) {
80+
public func observe(_ checkings: [Checking] = .defalut,
81+
highlights: [Highlight] = .defalut,
82+
with callback: @escaping (Checking.Result) -> Void) {
83+
var temp = base.checkings
84+
checkings.forEach { temp[$0] = (highlights, { callback($0.1) }) }
85+
base.checkings = temp
86+
}
87+
/// 添加监听
88+
/// - Parameters:
89+
/// - checkings: 检查类型
90+
/// - highlights: 高亮样式
91+
/// - callback: 触发回调
92+
public func observe(_ checkings: [Checking] = .defalut,
93+
highlights: [Highlight] = .defalut,
94+
with callback: @escaping (NSRange, Checking.Result) -> Void) {
8095
var temp = base.checkings
81-
checkings.forEach { temp[$0] = (highlights, callback) }
96+
checkings.forEach { temp[$0] = (highlights, { callback($0.0, $0.1) }) }
8297
base.checkings = temp
8398
}
8499

@@ -120,16 +135,16 @@ extension AttributedStringWrapper where Base: NSTextField {
120135
case .action(let result):
121136
guard var action = actions[range] else { return }
122137
action.handle = {
123-
action.callback(result)
124-
checkings[type]?.1(.action(result))
138+
action.callback(.init(range: range, content: result))
139+
checkings[type]?.1((range, .action(result)))
125140
}
126141
base.actions[range] = action
127142

128143
default:
129144
guard let value = checkings[type] else { return }
130145
var action = Action(.click, highlights: value.0)
131146
action.handle = {
132-
value.1(result)
147+
value.1((range, result))
133148
}
134149
base.actions[range] = action
135150
}
@@ -178,7 +193,7 @@ extension NSTextField {
178193
fileprivate typealias Action = AttributedString.Action
179194
fileprivate typealias Checking = AttributedString.Checking
180195
fileprivate typealias Highlight = AttributedString.Action.Highlight
181-
fileprivate typealias Checkings = [Checking: ([Highlight], (Checking.Result) -> Void)]
196+
fileprivate typealias Checkings = [Checking: ([Highlight], ((NSRange, Checking.Result)) -> Void)]
182197

183198
/// 是否启用Action
184199
fileprivate var isActionEnabled: Bool {

Sources/Extension/UIKit/UILabelExtension.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,33 @@ extension AttributedStringWrapper where Base: UILabel {
101101
/// - checking: 检查类型
102102
/// - highlights: 高亮样式
103103
/// - callback: 触发回调
104-
public func observe(_ checking: Checking, highlights: [Highlight] = .defalut, with callback: @escaping (Checking.Result) -> Void) {
104+
public func observe(_ checking: Checking,
105+
highlights: [Highlight] = .defalut,
106+
with callback: @escaping (Checking.Result) -> Void) {
105107
observe([checking], highlights: highlights, with: callback)
106108
}
107109
/// 添加监听
108110
/// - Parameters:
109111
/// - checkings: 检查类型
110112
/// - highlights: 高亮样式
111113
/// - callback: 触发回调
112-
public func observe(_ checkings: [Checking] = .defalut, highlights: [Highlight] = .defalut, with callback: @escaping (Checking.Result) -> Void) {
114+
public func observe(_ checkings: [Checking] = .defalut,
115+
highlights: [Highlight] = .defalut,
116+
with callback: @escaping (Checking.Result) -> Void) {
113117
var temp = base.checkings
114-
checkings.forEach { temp[$0] = (highlights, callback) }
118+
checkings.forEach { temp[$0] = (highlights, { callback($0.1) }) }
119+
base.checkings = temp
120+
}
121+
/// 添加监听
122+
/// - Parameters:
123+
/// - checkings: 检查类型
124+
/// - highlights: 高亮样式
125+
/// - callback: 触发回调
126+
public func observe(_ checkings: [Checking] = .defalut,
127+
highlights: [Highlight] = .defalut,
128+
with callback: @escaping (NSRange, Checking.Result) -> Void) {
129+
var temp = base.checkings
130+
checkings.forEach { temp[$0] = (highlights, { callback($0.0, $0.1) }) }
115131
base.checkings = temp
116132
}
117133

@@ -153,16 +169,16 @@ extension AttributedStringWrapper where Base: UILabel {
153169
case .action(let result):
154170
guard var action = actions[range] else { return }
155171
action.handle = {
156-
action.callback(result)
157-
checkings[type]?.1(.action(result))
172+
action.callback(.init(range: range, content: result))
173+
checkings[type]?.1((range, .action(result)))
158174
}
159175
base.actions[range] = action
160176

161177
default:
162178
guard let value = checkings[type] else { return }
163179
var action = Action(.click, highlights: value.0)
164180
action.handle = {
165-
value.1(result)
181+
value.1((range, result))
166182
}
167183
base.actions[range] = action
168184
}
@@ -203,7 +219,7 @@ extension UILabel {
203219
fileprivate typealias Action = AttributedString.Action
204220
fileprivate typealias Checking = AttributedString.Checking
205221
fileprivate typealias Highlight = AttributedString.Action.Highlight
206-
fileprivate typealias Checkings = [Checking: ([Highlight], (Checking.Result) -> Void)]
222+
fileprivate typealias Checkings = [Checking: ([Highlight], ((NSRange, Checking.Result)) -> Void)]
207223

208224
/// 是否启用Action
209225
fileprivate var isActionEnabled: Bool {

Sources/Extension/UIKit/UITextViewExtension.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,33 @@ extension AttributedStringWrapper where Base: UITextView {
7575
/// - checking: 检查类型
7676
/// - highlights: 高亮样式
7777
/// - callback: 触发回调
78-
public func observe(_ checking: Checking, highlights: [Highlight] = .defalut, with callback: @escaping (Checking.Result) -> Void) {
78+
public func observe(_ checking: Checking,
79+
highlights: [Highlight] = .defalut,
80+
with callback: @escaping (Checking.Result) -> Void) {
7981
observe([checking], highlights: highlights, with: callback)
8082
}
81-
8283
/// 添加监听
8384
/// - Parameters:
8485
/// - checkings: 检查类型
8586
/// - highlights: 高亮样式
8687
/// - callback: 触发回调
87-
public func observe(_ checkings: [Checking] = .defalut, highlights: [Highlight] = .defalut, with callback: @escaping (Checking.Result) -> Void) {
88+
public func observe(_ checkings: [Checking] = .defalut,
89+
highlights: [Highlight] = .defalut,
90+
with callback: @escaping (Checking.Result) -> Void) {
91+
var temp = base.checkings
92+
checkings.forEach { temp[$0] = (highlights, { callback($0.1) }) }
93+
base.checkings = temp
94+
}
95+
/// 添加监听
96+
/// - Parameters:
97+
/// - checkings: 检查类型
98+
/// - highlights: 高亮样式
99+
/// - callback: 触发回调
100+
public func observe(_ checkings: [Checking] = .defalut,
101+
highlights: [Highlight] = .defalut,
102+
with callback: @escaping (NSRange, Checking.Result) -> Void) {
88103
var temp = base.checkings
89-
checkings.forEach { temp[$0] = (highlights, callback) }
104+
checkings.forEach { temp[$0] = (highlights, { callback($0.0, $0.1) }) }
90105
base.checkings = temp
91106
}
92107

@@ -130,16 +145,16 @@ extension AttributedStringWrapper where Base: UITextView {
130145
case .action(let result):
131146
guard var action = actions[range] else { return }
132147
action.handle = {
133-
action.callback(result)
134-
checkings[type]?.1(.action(result))
148+
action.callback(.init(range: range, content: result))
149+
checkings[type]?.1((range, .action(result)))
135150
}
136151
base.actions[range] = action
137152

138153
default:
139154
guard let value = checkings[type] else { return }
140155
var action = Action(.click, highlights: value.0)
141156
action.handle = {
142-
value.1(result)
157+
value.1((range, result))
143158
}
144159
base.actions[range] = action
145160
}
@@ -218,7 +233,7 @@ extension UITextView {
218233
fileprivate typealias Action = AttributedString.Action
219234
fileprivate typealias Checking = AttributedString.Checking
220235
fileprivate typealias Highlight = AttributedString.Action.Highlight
221-
fileprivate typealias Checkings = [Checking: ([Highlight], (Checking.Result) -> Void)]
236+
fileprivate typealias Checkings = [Checking: ([Highlight], ((NSRange, Checking.Result)) -> Void)]
222237

223238
/// 是否启用Action
224239
fileprivate var isActionEnabled: Bool {

0 commit comments

Comments
 (0)