Skip to content

Commit af82ebf

Browse files
committed
完成Action的高亮和触发方式支持, 修复UILabel点击计算错误的问题.
1 parent 46592c6 commit af82ebf

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
lines changed

Demo/Demo/Details/ActionViewController.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ class ActionViewController: UIViewController {
2828
}
2929
}
3030

31-
let action = AttributedString.Action(.press, highlights: .defalut) { (result) in
31+
func press(_ result: AttributedString.Action.Result) {
32+
switch result.content {
33+
case .string(let value):
34+
print("按住了文本: \n\(value) \nrange: \(result.range)")
35+
36+
case .attachment(let value):
37+
print("按住了附件: \n\(value) \nrange: \(result.range)")
38+
}
39+
}
40+
41+
let action = AttributedString.Action(.press, highlights: [.background(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))]) { (result) in
3242
switch result.content {
3343
case .string(let value):
3444
print("点击了文本: \n\(value) \nrange: \(result.range)")
@@ -43,7 +53,9 @@ class ActionViewController: UIViewController {
4353
4454
This is a picture -> \(.image(#imageLiteral(resourceName: "huaji"), .custom(size: .init(width: 100, height: 100))), action: click) -> Displayed in custom size.
4555
46-
This is \("Long Press", .font(.systemFont(ofSize: 30)), .action(action))
56+
This is \("Long Press", .font(.systemFont(ofSize: 30)), .action(.press, press))
57+
58+
Please long press -> \(.image(#imageLiteral(resourceName: "swift-icon"), .original(.center)), action: action)
4759
4860
"""
4961

@@ -52,7 +64,10 @@ class ActionViewController: UIViewController {
5264
5365
This is a picture -> \(.image(#imageLiteral(resourceName: "huaji"), .custom(size: .init(width: 100, height: 100))), action: click) -> Displayed in custom size.
5466
55-
This is \("Long Press", .font(.systemFont(ofSize: 30)), .action(action))
67+
This is \("Long Press", .font(.systemFont(ofSize: 30)), .action(.press, press))
68+
69+
Please long press -> \(.image(#imageLiteral(resourceName: "swift-icon"), .original(.center)), action: action)
70+
5671
"""
5772
}
5873
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ let e: AttributedString = "\("lee", .action(click))"
186186
let f: AttributedString = "\(.image(image), action: click)"
187187

188188
// More information.
189-
func click(_ action: AttributedString.Action) {
190-
switch action.content {
189+
func click(_ result: AttributedString.Action.Result) {
190+
switch result.content {
191191
case .string(let value):
192-
print("Currently clicked text: \(value) range: \(action.range)")
192+
print("Currently clicked text: \(value) range: \(result.range)")
193193

194194
case .attachment(let value):
195-
print("Currently clicked attachment: \(value) range: \(action.range)")
195+
print("Currently clicked attachment: \(value) range: \(result.range)")
196196
}
197197
}
198198

README_CN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ let e: AttributedString = "\("lee", .action(click))"
186186
let f: AttributedString = "\(.image(image), action: click)"
187187

188188
// 获取更多信息
189-
func click(_ action: AttributedString.Action) {
190-
switch action.content {
189+
func click(_ result: AttributedString.Action.Result) {
190+
switch result.content {
191191
case .string(let value):
192-
print("点击了文本: \(value) range: \(action.range)")
192+
print("点击了文本: \(value) range: \(result.range)")
193193

194194
case .attachment(let value):
195-
print("点击了附件: \(value) range: \(action.range)")
195+
print("点击了附件: \(value) range: \(result.range)")
196196
}
197197
}
198198

Sources/Action.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extension AttributedString.Action.Result {
7777
extension AttributedString.Attribute {
7878

7979
public typealias Action = AttributedString.Action
80+
public typealias Trigger = Action.Trigger
8081
public typealias Highlight = Action.Highlight
8182
public typealias Result = Action.Result
8283

@@ -89,6 +90,10 @@ extension AttributedString.Attribute {
8990
return .init(attributes: [.action: Action(with: value)])
9091
}
9192

93+
public static func action(_ trigger: Trigger, _ closure: @escaping (Result) -> Void) -> Self {
94+
return .init(attributes: [.action: Action(trigger, with: closure)])
95+
}
96+
9297
public static func action(_ highlights: [Highlight], _ closure: @escaping (Result) -> Void) -> Self {
9398
return .init(attributes: [.action: Action(highlights: highlights, with: closure)])
9499
}
@@ -123,11 +128,24 @@ extension AttributedString {
123128
public init(_ attachment: Attachment, action: @escaping (Action.Result) -> Void) {
124129
self.value = AttributedString(attachment.value, action: action).value
125130
}
131+
132+
public init<T: NSTextAttachment>(_ attachment: T, action: Action) {
133+
self.value = AttributedString(.init(attachment: attachment), .action(action)).value
134+
}
135+
136+
public init(_ attachment: ImageTextAttachment, action: Action) {
137+
self.value = AttributedString(.init(attachment: attachment), .action(action)).value
138+
}
139+
140+
public init(_ attachment: Attachment, action: Action) {
141+
self.value = AttributedString(attachment.value, action: action).value
142+
}
126143
}
127144

128145
extension AttributedStringInterpolation {
129146

130-
public typealias Result = AttributedString.Action.Result
147+
public typealias Action = AttributedString.Action
148+
public typealias Result = Action.Result
131149

132150
public mutating func appendInterpolation(_ value: ImageTextAttachment, action: @escaping () -> Void) {
133151
self.value.append(AttributedString(.init(attachment: value), .action(action)).value)
@@ -144,6 +162,14 @@ extension AttributedStringInterpolation {
144162
public mutating func appendInterpolation(_ value: Attachment, action: @escaping (Result) -> Void) {
145163
self.value.append(AttributedString(.init(attachment: value.value), .action(action)).value)
146164
}
165+
166+
public mutating func appendInterpolation(_ value: ImageTextAttachment, action: Action) {
167+
self.value.append(AttributedString(.init(attachment: value), .action(action)).value)
168+
}
169+
170+
public mutating func appendInterpolation(_ value: Attachment, action: Action) {
171+
self.value.append(AttributedString(.init(attachment: value.value), .action(action)).value)
172+
}
147173
}
148174

149175
extension AttributedString.Action.Highlight {

Sources/Extension/AppKit/NSTextFieldExtension.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ extension AttributedStringWrapper where Base: NSTextField {
2323
public var string: AttributedString {
2424
get { AttributedString(base.attributedStringValue) }
2525
set {
26-
base.attributedStringValue = newValue.value
26+
base.attributedStringValue = AttributedString(
27+
newValue.value,
28+
.font(base.font!),
29+
.paragraph(
30+
.alignment(base.alignment),
31+
.baseWritingDirection(base.baseWritingDirection)
32+
)
33+
).value
2734

2835
setupGestureRecognizers()
2936
}
@@ -143,10 +150,7 @@ fileprivate extension NSTextField {
143150
}
144151

145152
func matching(_ point: CGPoint) -> (NSRange, Action)? {
146-
// 同步NSTextField默认样式 使用嵌入包装模式 防止原有富文本样式被覆盖
147-
let attributedString: AttributedString = """
148-
\(wrap: .embedding(.init(attributedStringValue)), with: [.font(font!), .paragraph(.alignment(alignment), .baseWritingDirection(baseWritingDirection))])
149-
"""
153+
let attributedString = AttributedString(attributedStringValue)
150154

151155
// 构建同步Label设置的TextKit
152156
let textStorage = NSTextStorage(attributedString: attributedString.value)

Sources/Extension/UIKit/UILabelExtension.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ extension UILabel: AttributedStringCompatible {
2323
}
2424

2525
extension AttributedStringWrapper where Base: UILabel {
26-
26+
2727
public var text: AttributedString? {
2828
get { AttributedString(base.attributedText) }
2929
set {
30-
base.attributedText = newValue?.value
30+
base.attributedText = nil
31+
// UILabel 需要先将attributedText置为空 才能拿到真实的默认字体与对齐方式等
32+
base.attributedText = AttributedString(
33+
newValue?.value,
34+
.font(base.font),
35+
.paragraph(.alignment(base.textAlignment))
36+
)?.value
3137

3238
#if os(iOS)
3339
setupGestureRecognizers()
@@ -138,11 +144,7 @@ fileprivate extension UILabel {
138144
}
139145

140146
func matching(_ point: CGPoint) -> (NSRange, Action)? {
141-
guard let attributedText = attributedText else { return nil }
142-
// 同步Label默认样式 使用嵌入包装模式 防止原有富文本样式被覆盖
143-
let attributedString: AttributedString = """
144-
\(wrap: .embedding(.init(attributedText)), with: [.font(font), .paragraph(.alignment(textAlignment))])
145-
"""
147+
guard let attributedString = AttributedString(attributedText) else { return nil }
146148

147149
// 构建同步Label设置的TextKit
148150
let textStorage = NSTextStorage(attributedString: attributedString.value)

0 commit comments

Comments
 (0)