From 29ef7c1eba154063654eb22192c313a3d1cc1dd6 Mon Sep 17 00:00:00 2001 From: LEE <18611401994@163.com> Date: Mon, 14 Mar 2022 11:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=88=E5=B9=B6action?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Demo/Details/CheckingViewController.swift | 22 +++++++++++++ Sources/Action.swift | 31 +++++++++++++++---- Sources/Attribute.swift | 8 ----- Sources/AttributedString.swift | 24 +++++++------- Sources/Checking.swift | 12 +++++++ 5 files changed, 72 insertions(+), 25 deletions(-) diff --git a/Demo/Demo/Details/CheckingViewController.swift b/Demo/Demo/Details/CheckingViewController.swift index 01b91c6..4873531 100644 --- a/Demo/Demo/Details/CheckingViewController.swift +++ b/Demo/Demo/Details/CheckingViewController.swift @@ -42,6 +42,17 @@ class CheckingViewController: ViewController { 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 } @@ -54,6 +65,17 @@ class CheckingViewController: ViewController { 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 } diff --git a/Sources/Action.swift b/Sources/Action.swift index 558b8d6..dee765b 100644 --- a/Sources/Action.swift +++ b/Sources/Action.swift @@ -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]) } @@ -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 { + 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 { diff --git a/Sources/Attribute.swift b/Sources/Attribute.swift index ef68bfc..c2b3ddf 100644 --- a/Sources/Attribute.swift +++ b/Sources/Attribute.swift @@ -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]) - } -} diff --git a/Sources/AttributedString.swift b/Sources/AttributedString.swift index cf62e60..9168a48 100644 --- a/Sources/AttributedString.swift +++ b/Sources/AttributedString.swift @@ -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 @@ -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) @@ -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) diff --git a/Sources/Checking.swift b/Sources/Checking.swift index 190471b..1bf4993 100644 --- a/Sources/Checking.swift +++ b/Sources/Checking.swift @@ -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 }) } @@ -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 }) }