Skip to content

Commit

Permalink
优化合并action处理
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang1994 committed Mar 14, 2022
1 parent de73a26 commit 29ef7c1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 25 deletions.
22 changes: 22 additions & 0 deletions Demo/Demo/Details/CheckingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class CheckingViewController: ViewController<CheckingView> {
string.add(attributes: [.foreground(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.phoneNumber])
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.link])
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.date])

// 测试action
// string.add(attributes: [.action {
// print("11")
// }], range: .init(location: 3, length: 6))
// string.add(attributes: [.action {
// print("22")
// },.action {
// print("33")
// }], checkings: [.link])

container.label.attributed.text = string
}

Expand All @@ -54,6 +65,17 @@ class CheckingViewController: ViewController<CheckingView> {
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1))], checkings: [.date])
string.add(attributes: [.foreground(#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1))], checkings: [.regex("Li Xiang")])
string.add(attributes: [.font(.systemFont(ofSize: 16, weight: .medium))], checkings: [.action])

// 测试action
// string.add(attributes: [.action {
// print("11")
// }], range: .init(location: 3, length: 6))
// string.add(attributes: [.action {
// print("22")
// },.action {
// print("33")
// }], checkings: [.link])

container.textView.attributed.text = string
}

Expand Down
31 changes: 25 additions & 6 deletions Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,7 @@ extension ASAttributedStringInterpolation {
}

extension ASAttributedString.Action.Highlight {

@available(*, deprecated, message: "use foreground(_:)", renamed: "foreground(_:)")
public static func color(_ value: Color) -> Self {
return .init(attributes: [.foregroundColor: value])
}


public static func foreground(_ value: Color) -> Self {
return .init(attributes: [.foregroundColor: value])
}
Expand Down Expand Up @@ -269,6 +264,30 @@ 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 {
Expand Down
8 changes: 0 additions & 8 deletions Sources/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,3 @@ extension ASAttributedStringInterpolation {
self.value.append(ASAttributedString(wrap: mode, with: attributes).value)
}
}

extension ASAttributedString.Attribute {

@available(*, deprecated, message: "use foreground(_:)", renamed: "foreground(_:)")
public static func color(_ value: Color) -> Self {
return .init(attributes: [.foregroundColor: value])
}
}
24 changes: 13 additions & 11 deletions Sources/AttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,7 @@ public struct ASAttributedString {

#if os(iOS) || os(macOS)
// 合并多个Action
var attributes = attributes

let actions = attributes.compactMap {
$0.attributes[.action] as? Attribute.Action
}
if !actions.isEmpty {
attributes.removeAll(where: {
$0.attributes.keys.contains(.action)
})
attributes.append(.init(attributes: [.action: actions]))
}
let attributes = attributes.mergedAction()

#endif

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

#if os(iOS) || os(macOS)
// 合并多个Action
let attributes = attributes.mergedAction()

#endif

var temp: [NSAttributedString.Key: Any] = [:]
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
let string = NSMutableAttributedString(attributedString: value)
Expand All @@ -194,6 +190,12 @@ extension ASAttributedString {
public mutating func set(attributes: [Attribute], range: NSRange) {
guard !attributes.isEmpty, range.length > 0 else { return }

#if os(iOS) || os(macOS)
// 合并多个Action
let attributes = attributes.mergedAction()

#endif

var temp: [NSAttributedString.Key: Any] = [:]
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
let string = NSMutableAttributedString(attributedString: value)
Expand Down
12 changes: 12 additions & 0 deletions Sources/Checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ extension ASAttributedString {
public mutating func add(attributes: [Attribute], checkings: [Checking] = .defalut) {
guard !attributes.isEmpty, !checkings.isEmpty else { return }

#if os(iOS) || os(macOS)
// 合并多个Action
let attributes = attributes.mergedAction()

#endif

var temp: [NSAttributedString.Key: Any] = [:]
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }

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

#if os(iOS) || os(macOS)
// 合并多个Action
let attributes = attributes.mergedAction()

#endif

var temp: [NSAttributedString.Key: Any] = [:]
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }

Expand Down

0 comments on commit 29ef7c1

Please sign in to comment.