Skip to content

Commit 29ef7c1

Browse files
committed
优化合并action处理
1 parent de73a26 commit 29ef7c1

File tree

5 files changed

+72
-25
lines changed

5 files changed

+72
-25
lines changed

Demo/Demo/Details/CheckingViewController.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ class CheckingViewController: ViewController<CheckingView> {
4242
string.add(attributes: [.foreground(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.phoneNumber])
4343
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.link])
4444
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.date])
45+
46+
// 测试action
47+
// string.add(attributes: [.action {
48+
// print("11")
49+
// }], range: .init(location: 3, length: 6))
50+
// string.add(attributes: [.action {
51+
// print("22")
52+
// },.action {
53+
// print("33")
54+
// }], checkings: [.link])
55+
4556
container.label.attributed.text = string
4657
}
4758

@@ -54,6 +65,17 @@ class CheckingViewController: ViewController<CheckingView> {
5465
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1))], checkings: [.date])
5566
string.add(attributes: [.foreground(#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1))], checkings: [.regex("Li Xiang")])
5667
string.add(attributes: [.font(.systemFont(ofSize: 16, weight: .medium))], checkings: [.action])
68+
69+
// 测试action
70+
// string.add(attributes: [.action {
71+
// print("11")
72+
// }], range: .init(location: 3, length: 6))
73+
// string.add(attributes: [.action {
74+
// print("22")
75+
// },.action {
76+
// print("33")
77+
// }], checkings: [.link])
78+
5779
container.textView.attributed.text = string
5880
}
5981

Sources/Action.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,7 @@ extension ASAttributedStringInterpolation {
188188
}
189189

190190
extension ASAttributedString.Action.Highlight {
191-
192-
@available(*, deprecated, message: "use foreground(_:)", renamed: "foreground(_:)")
193-
public static func color(_ value: Color) -> Self {
194-
return .init(attributes: [.foregroundColor: value])
195-
}
196-
191+
197192
public static func foreground(_ value: Color) -> Self {
198193
return .init(attributes: [.foregroundColor: value])
199194
}
@@ -269,6 +264,30 @@ extension NSAttributedString.Key {
269264
static let action = NSAttributedString.Key("com.attributed.string.action")
270265
}
271266

267+
extension Array where Element == ASAttributedString.Attribute {
268+
269+
/// 合并Action 当存在多个action时 将所有action合并到一个数组中
270+
/// - Returns: 合并后的数组
271+
func mergedAction() -> Array<Element> {
272+
var temp = self
273+
274+
var actions = temp.compactMap {
275+
$0.attributes[.action] as? ASAttributedString.Attribute.Action
276+
}
277+
actions.append(contentsOf: temp.compactMap {
278+
$0.attributes[.action] as? [ASAttributedString.Attribute.Action]
279+
}.flatMap({ $0 }))
280+
281+
if !actions.isEmpty {
282+
temp.removeAll(where: {
283+
$0.attributes.keys.contains(.action)
284+
})
285+
temp.append(.init(attributes: [.action: actions]))
286+
}
287+
return temp
288+
}
289+
}
290+
272291
#endif
273292

274293
extension NSAttributedString {

Sources/Attribute.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,3 @@ extension ASAttributedStringInterpolation {
188188
self.value.append(ASAttributedString(wrap: mode, with: attributes).value)
189189
}
190190
}
191-
192-
extension ASAttributedString.Attribute {
193-
194-
@available(*, deprecated, message: "use foreground(_:)", renamed: "foreground(_:)")
195-
public static func color(_ value: Color) -> Self {
196-
return .init(attributes: [.foregroundColor: value])
197-
}
198-
}

Sources/AttributedString.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,7 @@ public struct ASAttributedString {
9191

9292
#if os(iOS) || os(macOS)
9393
// 合并多个Action
94-
var attributes = attributes
95-
96-
let actions = attributes.compactMap {
97-
$0.attributes[.action] as? Attribute.Action
98-
}
99-
if !actions.isEmpty {
100-
attributes.removeAll(where: {
101-
$0.attributes.keys.contains(.action)
102-
})
103-
attributes.append(.init(attributes: [.action: actions]))
104-
}
94+
let attributes = attributes.mergedAction()
10595

10696
#endif
10797

@@ -184,6 +174,12 @@ extension ASAttributedString {
184174
public mutating func add(attributes: [Attribute], range: NSRange) {
185175
guard !attributes.isEmpty, range.length > 0 else { return }
186176

177+
#if os(iOS) || os(macOS)
178+
// 合并多个Action
179+
let attributes = attributes.mergedAction()
180+
181+
#endif
182+
187183
var temp: [NSAttributedString.Key: Any] = [:]
188184
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
189185
let string = NSMutableAttributedString(attributedString: value)
@@ -194,6 +190,12 @@ extension ASAttributedString {
194190
public mutating func set(attributes: [Attribute], range: NSRange) {
195191
guard !attributes.isEmpty, range.length > 0 else { return }
196192

193+
#if os(iOS) || os(macOS)
194+
// 合并多个Action
195+
let attributes = attributes.mergedAction()
196+
197+
#endif
198+
197199
var temp: [NSAttributedString.Key: Any] = [:]
198200
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
199201
let string = NSMutableAttributedString(attributedString: value)

Sources/Checking.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ extension ASAttributedString {
129129
public mutating func add(attributes: [Attribute], checkings: [Checking] = .defalut) {
130130
guard !attributes.isEmpty, !checkings.isEmpty else { return }
131131

132+
#if os(iOS) || os(macOS)
133+
// 合并多个Action
134+
let attributes = attributes.mergedAction()
135+
136+
#endif
137+
132138
var temp: [NSAttributedString.Key: Any] = [:]
133139
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
134140

@@ -141,6 +147,12 @@ extension ASAttributedString {
141147
public mutating func set(attributes: [Attribute], checkings: [Checking] = .defalut) {
142148
guard !attributes.isEmpty, !checkings.isEmpty else { return }
143149

150+
#if os(iOS) || os(macOS)
151+
// 合并多个Action
152+
let attributes = attributes.mergedAction()
153+
154+
#endif
155+
144156
var temp: [NSAttributedString.Key: Any] = [:]
145157
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
146158

0 commit comments

Comments
 (0)