Skip to content

Commit aca8827

Browse files
committed
重构Action, 支持多Action设置以及点击长按共存.
1 parent 51173fa commit aca8827

File tree

11 files changed

+380
-223
lines changed

11 files changed

+380
-223
lines changed

AttributedString.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AttributedString"
4-
s.version = "2.2.2"
4+
s.version = "3.0.0"
55
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"
66

77
s.homepage = "https://github.com/lixiang1994/AttributedString"

AttributedString.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@
833833
APPLICATION_EXTENSION_API_ONLY = YES;
834834
CODE_SIGN_STYLE = Automatic;
835835
DEFINES_MODULE = YES;
836-
DEVELOPMENT_TEAM = B9D8DJR5J5;
836+
DEVELOPMENT_TEAM = "";
837837
DYLIB_COMPATIBILITY_VERSION = 1;
838838
DYLIB_CURRENT_VERSION = 1;
839839
DYLIB_INSTALL_NAME_BASE = "@rpath";
@@ -861,7 +861,7 @@
861861
APPLICATION_EXTENSION_API_ONLY = YES;
862862
CODE_SIGN_STYLE = Automatic;
863863
DEFINES_MODULE = YES;
864-
DEVELOPMENT_TEAM = B9D8DJR5J5;
864+
DEVELOPMENT_TEAM = "";
865865
DYLIB_COMPATIBILITY_VERSION = 1;
866866
DYLIB_CURRENT_VERSION = 1;
867867
DYLIB_INSTALL_NAME_BASE = "@rpath";
@@ -888,7 +888,7 @@
888888
buildSettings = {
889889
CODE_SIGN_STYLE = Automatic;
890890
DEFINES_MODULE = YES;
891-
DEVELOPMENT_TEAM = B9D8DJR5J5;
891+
DEVELOPMENT_TEAM = "";
892892
DYLIB_COMPATIBILITY_VERSION = 1;
893893
DYLIB_CURRENT_VERSION = 1;
894894
DYLIB_INSTALL_NAME_BASE = "@rpath";
@@ -915,7 +915,7 @@
915915
buildSettings = {
916916
CODE_SIGN_STYLE = Automatic;
917917
DEFINES_MODULE = YES;
918-
DEVELOPMENT_TEAM = B9D8DJR5J5;
918+
DEVELOPMENT_TEAM = "";
919919
DYLIB_COMPATIBILITY_VERSION = 1;
920920
DYLIB_CURRENT_VERSION = 1;
921921
DYLIB_INSTALL_NAME_BASE = "@rpath";
@@ -943,7 +943,7 @@
943943
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
944944
CLANG_ENABLE_MODULES = YES;
945945
CODE_SIGN_STYLE = Automatic;
946-
DEVELOPMENT_TEAM = B9D8DJR5J5;
946+
DEVELOPMENT_TEAM = "";
947947
INFOPLIST_FILE = Tests/Info.plist;
948948
LD_RUNPATH_SEARCH_PATHS = (
949949
"$(inherited)",
@@ -967,7 +967,7 @@
967967
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
968968
CLANG_ENABLE_MODULES = YES;
969969
CODE_SIGN_STYLE = Automatic;
970-
DEVELOPMENT_TEAM = B9D8DJR5J5;
970+
DEVELOPMENT_TEAM = "";
971971
INFOPLIST_FILE = Tests/Info.plist;
972972
LD_RUNPATH_SEARCH_PATHS = (
973973
"$(inherited)",

Demo-Mac/Demo-Mac.xcodeproj/project.pbxproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
9B34BD5C243DC33500932E6C /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
11-
9B34BD5D243DC33500932E6C /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1210
9B34BD6B243F16EE00932E6C /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6A243F16EE00932E6C /* TableViewCell.swift */; };
1311
9B34BD6D243F172E00932E6C /* AllViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6C243F172E00932E6C /* AllViewController.swift */; };
1412
9B6E89C123827C48009EBEBE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B6E89C023827C48009EBEBE /* AppDelegate.swift */; };
1513
9B6E89C323827C48009EBEBE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B6E89C223827C48009EBEBE /* ViewController.swift */; };
1614
9B6E89C523827C49009EBEBE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9B6E89C423827C49009EBEBE /* Assets.xcassets */; };
1715
9B6E89C823827C49009EBEBE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9B6E89C623827C49009EBEBE /* Main.storyboard */; };
16+
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
17+
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1818
/* End PBXBuildFile section */
1919

2020
/* Begin PBXCopyFilesBuildPhase section */
21-
9B34BD5E243DC33500932E6C /* Embed Frameworks */ = {
21+
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */ = {
2222
isa = PBXCopyFilesBuildPhase;
2323
buildActionMask = 2147483647;
2424
dstPath = "";
2525
dstSubfolderSpec = 10;
2626
files = (
27-
9B34BD5D243DC33500932E6C /* AttributedString.framework in Embed Frameworks */,
27+
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */,
2828
);
2929
name = "Embed Frameworks";
3030
runOnlyForDeploymentPostprocessing = 0;
@@ -49,7 +49,7 @@
4949
isa = PBXFrameworksBuildPhase;
5050
buildActionMask = 2147483647;
5151
files = (
52-
9B34BD5C243DC33500932E6C /* AttributedString.framework in Frameworks */,
52+
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */,
5353
);
5454
runOnlyForDeploymentPostprocessing = 0;
5555
};
@@ -114,7 +114,7 @@
114114
9B6E89B923827C48009EBEBE /* Sources */,
115115
9B6E89BA23827C48009EBEBE /* Frameworks */,
116116
9B6E89BB23827C48009EBEBE /* Resources */,
117-
9B34BD5E243DC33500932E6C /* Embed Frameworks */,
117+
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */,
118118
);
119119
buildRules = (
120120
);
@@ -314,10 +314,10 @@
314314
buildSettings = {
315315
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
316316
CODE_SIGN_ENTITLEMENTS = "Demo-Mac/Demo_Mac.entitlements";
317-
CODE_SIGN_IDENTITY = "Apple Development";
317+
CODE_SIGN_IDENTITY = "-";
318318
CODE_SIGN_STYLE = Automatic;
319319
COMBINE_HIDPI_IMAGES = YES;
320-
DEVELOPMENT_TEAM = B9D8DJR5J5;
320+
DEVELOPMENT_TEAM = "";
321321
ENABLE_HARDENED_RUNTIME = YES;
322322
INFOPLIST_FILE = "Demo-Mac/Info.plist";
323323
LD_RUNPATH_SEARCH_PATHS = (
@@ -336,10 +336,10 @@
336336
buildSettings = {
337337
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
338338
CODE_SIGN_ENTITLEMENTS = "Demo-Mac/Demo_Mac.entitlements";
339-
CODE_SIGN_IDENTITY = "Apple Development";
339+
CODE_SIGN_IDENTITY = "-";
340340
CODE_SIGN_STYLE = Automatic;
341341
COMBINE_HIDPI_IMAGES = YES;
342-
DEVELOPMENT_TEAM = B9D8DJR5J5;
342+
DEVELOPMENT_TEAM = "";
343343
ENABLE_HARDENED_RUNTIME = YES;
344344
INFOPLIST_FILE = "Demo-Mac/Info.plist";
345345
LD_RUNPATH_SEARCH_PATHS = (

Demo/Demo/Details/ActionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ActionViewController: UIViewController {
5252
}
5353

5454
label.attributed.text = """
55-
This is \("Label", .font(.systemFont(ofSize: 50)), .action(clicked))
55+
This is \("Label", .font(.systemFont(ofSize: 50)), .action(clicked), .action(.press, pressed))
5656
5757
This is a picture -> \(.image(#imageLiteral(resourceName: "huaji"), .custom(size: .init(width: 100, height: 100))), action: clicked) -> Displayed in custom size.
5858

Demo/Demo/Details/CheckingViewController.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class CheckingViewController: ViewController<CheckingView> {
1515
super.viewDidLoad()
1616

1717
// 添加电话号码类型监听
18-
container.label.attributed.observe([.phoneNumber], highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))]) { (result) in
18+
container.label.attributed.observe(.phoneNumber, with: .init(.click, highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))], with: { (result) in
1919
print(result)
20-
}
20+
}))
2121
// 添加默认类型监听
2222
container.textView.attributed.observe(highlights: [.foreground(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))]) { (result) in
2323
print(result)
@@ -42,7 +42,6 @@ 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-
string.add(attributes: [.font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.action])
4645
container.label.attributed.text = string
4746
}
4847

Sources/Action.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ extension ASAttributedString {
3636
/// 触发回调
3737
let callback: (Result) -> Void
3838

39-
/// 内部处理
39+
/// 内部使用
40+
internal var isExternal: Bool = true
4041
internal var handle: (() -> Void)?
4142

4243
public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) {
@@ -45,10 +46,11 @@ extension ASAttributedString {
4546
self.callback = callback
4647
}
4748

48-
init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut) {
49+
internal init(_ trigger: Trigger, _ highlights: [Highlight], _ callback: @escaping (Result) -> Void) {
4950
self.trigger = trigger
5051
self.highlights = highlights
51-
self.callback = { _ in }
52+
self.callback = callback
53+
self.isExternal = false
5254
}
5355
}
5456
}

Sources/AttributedString.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public struct ASAttributedString {
8989
return
9090
}
9191

92+
#if os(iOS) || os(macOS)
93+
// 合并多个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+
}
105+
106+
#endif
107+
92108
// 获取通用属性
93109
var temp: [NSAttributedString.Key: Any] = [:]
94110
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }

Sources/Checking.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension ASAttributedString.Checking {
4848
/// 正则表达式
4949
case regex(NSAttributedString)
5050
#if os(iOS) || os(macOS)
51-
case action(ASAttributedString.Action.Result.Content)
51+
case action([ASAttributedString.Action])
5252
#endif
5353
#if !os(watchOS)
5454
case attachment(NSTextAttachment)
@@ -61,6 +61,31 @@ extension ASAttributedString.Checking {
6161
}
6262
}
6363

64+
#if os(iOS) || os(macOS)
65+
66+
extension ASAttributedString.Checking {
67+
68+
public struct Action {
69+
public typealias Trigger = ASAttributedString.Action.Trigger
70+
public typealias Highlight = ASAttributedString.Action.Highlight
71+
72+
/// 触发类型
73+
let trigger: Trigger
74+
/// 高亮属性
75+
let highlights: [Highlight]
76+
/// 触发回调
77+
let callback: (Result) -> Void
78+
79+
public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) {
80+
self.trigger = trigger
81+
self.highlights = highlights
82+
self.callback = callback
83+
}
84+
}
85+
}
86+
87+
#endif
88+
6489
extension ASAttributedString.Checking.Result {
6590

6691
public struct Date {
@@ -168,15 +193,16 @@ extension ASAttributedString {
168193
let substring = value.attributedSubstring(from: match.range)
169194
result[match.range] = (checking, .regex(substring))
170195
}
171-
196+
172197
#if os(iOS) || os(macOS)
173198
case .action:
174-
let actions: [NSRange: ASAttributedString.Action] = value.get(.action)
175-
for action in actions where !contains(action.key) {
176-
result[action.key] = (.action, .action(value.get(action.key).content))
199+
let ranges: [NSRange: [Action]] = value.get(.action)
200+
for range in ranges where !contains(range.key) {
201+
let actions = range.value.filter({ $0.isExternal })
202+
result[range.key] = (.action, .action(actions))
177203
}
178204
#endif
179-
205+
180206
#if !os(watchOS)
181207
case .attachment:
182208
let attachments: [NSRange: NSTextAttachment] = value.get(.attachment)

0 commit comments

Comments
 (0)