From d1d1ff3942554a92d1fd2464e9e7582794009c6d Mon Sep 17 00:00:00 2001 From: LEE <18611401994@163.com> Date: Thu, 19 Sep 2024 15:56:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AttributedString.podspec | 2 +- Sources/Action.swift | 2 +- Sources/Extension/AppKit/NSTextFieldExtension.swift | 7 +++---- .../Extension/UIKit/UILabel/UILabelExtension.swift | 13 +++++++------ Sources/Extension/UIKit/UITextViewExtension.swift | 13 +++++++------ 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/AttributedString.podspec b/AttributedString.podspec index 13d6b56..0709b47 100644 --- a/AttributedString.podspec +++ b/AttributedString.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "AttributedString" -s.version = "3.4.1" +s.version = "3.4.2" s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤" s.homepage = "https://github.com/lixiang1994/AttributedString" diff --git a/Sources/Action.swift b/Sources/Action.swift index 615f13c..1690e39 100644 --- a/Sources/Action.swift +++ b/Sources/Action.swift @@ -38,7 +38,7 @@ extension ASAttributedString { /// 内部使用 internal var isExternal: Bool = true - internal var handle: (() -> Void)? + internal var result: Action.Result? public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) { self.trigger = trigger diff --git a/Sources/Extension/AppKit/NSTextFieldExtension.swift b/Sources/Extension/AppKit/NSTextFieldExtension.swift index eb8e3bd..408196a 100644 --- a/Sources/Extension/AppKit/NSTextFieldExtension.swift +++ b/Sources/Extension/AppKit/NSTextFieldExtension.swift @@ -174,9 +174,7 @@ extension ASAttributedStringWrapper where Base: NSTextField { let result: Action.Result = string.value.get($1.key) let actions: [Action] = $1.value.reduce(into: []) { var temp = $1 - temp.handle = { - temp.callback(result) - } + temp.result = result $0.append(temp) } $0[$1.key] = actions @@ -292,7 +290,8 @@ fileprivate extension NSTextField { guard let touched = self.touched else { return } let actions = touched.1.flatMap({ $0.value }) for action in actions where action.trigger.matching(sender) { - action.handle?() + guard let result = action.result else { return } + action.callback(result) } } diff --git a/Sources/Extension/UIKit/UILabel/UILabelExtension.swift b/Sources/Extension/UIKit/UILabel/UILabelExtension.swift index 8a7bbd0..d116605 100644 --- a/Sources/Extension/UIKit/UILabel/UILabelExtension.swift +++ b/Sources/Extension/UIKit/UILabel/UILabelExtension.swift @@ -181,9 +181,7 @@ extension ASAttributedStringWrapper where Base: UILabel { let result: Action.Result = string.value.get($1.key) let actions: [Action] = $1.value.reduce(into: []) { var temp = $1 - temp.handle = { - temp.callback(result) - } + temp.result = result $0.append(temp) } $0[$1.key] = actions @@ -286,7 +284,8 @@ extension UILabel { return } // 保证 touchesBegan -> Action -> touchesEnded 的调用顺序 - ActionQueue.main.ended { + ActionQueue.main.ended { [weak self] in + guard let self = self else { return } self.touched = nil self.attributedText = touched.0.value } @@ -300,7 +299,8 @@ extension UILabel { return } // 保证 touchesBegan -> Action -> touchesEnded 的调用顺序 - ActionQueue.main.cancelled { + ActionQueue.main.cancelled { [weak self] in + guard let self = self else { return } self.touched = nil self.attributedText = touched.0.value } @@ -319,7 +319,8 @@ fileprivate extension UILabel { guard let touched = self.touched else { return } let actions = touched.1.flatMap({ $0.value }) for action in actions where action.trigger.matching(sender) { - action.handle?() + guard let result = action.result else { return } + action.callback(result) } } } diff --git a/Sources/Extension/UIKit/UITextViewExtension.swift b/Sources/Extension/UIKit/UITextViewExtension.swift index fc19148..8efa6f4 100644 --- a/Sources/Extension/UIKit/UITextViewExtension.swift +++ b/Sources/Extension/UIKit/UITextViewExtension.swift @@ -184,9 +184,7 @@ extension ASAttributedStringWrapper where Base: UITextView { let result: Action.Result = string.value.get($1.key) let actions: [Action] = $1.value.reduce(into: []) { var temp = $1 - temp.handle = { - temp.callback(result) - } + temp.result = result $0.append(temp) } $0[$1.key] = actions @@ -327,7 +325,8 @@ extension UITextView { return } // 保证 touchesBegan -> Action -> touchesEnded 的调用顺序 - ActionQueue.main.ended { + ActionQueue.main.ended { [weak self] in + guard let self = self else { return } self.touched = nil self.attributedText = touched.0.value self.layout() @@ -342,7 +341,8 @@ extension UITextView { return } // 保证 touchesBegan -> Action -> touchesEnded 的调用顺序 - ActionQueue.main.cancelled { + ActionQueue.main.cancelled { [weak self] in + guard let self = self else { return } self.touched = nil self.attributedText = touched.0.value self.layout() @@ -362,7 +362,8 @@ fileprivate extension UITextView { guard let touched = self.touched else { return } let actions = touched.1.flatMap({ $0.value }) for action in actions where action.trigger.matching(sender) { - action.handle?() + guard let result = action.result else { return } + action.callback(result) } } }