Skip to content

Commit ae3e7df

Browse files
committed
增加 根据String类型初始化方法, 优化匹配逻辑, 优化代码结构等
1 parent ecec215 commit ae3e7df

File tree

10 files changed

+204
-148
lines changed

10 files changed

+204
-148
lines changed

Demo-TV/Demo-TV/AllTableViewController.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,39 +173,39 @@ extension AllTableViewController {
173173
title: ("ForegroundColor", "字色"),
174174
content: """
175175
176-
\("foregroundColor", .color(.white))
176+
\("foregroundColor", .foreground(.white))
177177
178-
\("foregroundColor", .color(#colorLiteral(red: 0.7952535152, green: 0.7952535152, blue: 0.7952535152, alpha: 1)))
178+
\("foregroundColor", .foreground(#colorLiteral(red: 0.7952535152, green: 0.7952535152, blue: 0.7952535152, alpha: 1)))
179179
180-
\("foregroundColor", .color(#colorLiteral(red: 0.5723067522, green: 0.5723067522, blue: 0.5723067522, alpha: 1)))
180+
\("foregroundColor", .foreground(#colorLiteral(red: 0.5723067522, green: 0.5723067522, blue: 0.5723067522, alpha: 1)))
181181
182-
\("foregroundColor", .color(#colorLiteral(red: 0.3179988265, green: 0.3179988265, blue: 0.3179988265, alpha: 1)))
182+
\("foregroundColor", .foreground(#colorLiteral(red: 0.3179988265, green: 0.3179988265, blue: 0.3179988265, alpha: 1)))
183183
184-
\("foregroundColor", .color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)))
184+
\("foregroundColor", .foreground(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)))
185185
186-
\("foregroundColor", .color(#colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1)))
186+
\("foregroundColor", .foreground(#colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1)))
187187
188-
\("foregroundColor", .color(#colorLiteral(red: 1, green: 0.8323456645, blue: 0.4732058644, alpha: 1)))
188+
\("foregroundColor", .foreground(#colorLiteral(red: 1, green: 0.8323456645, blue: 0.4732058644, alpha: 1)))
189189
190-
\("foregroundColor", .color(#colorLiteral(red: 0.9995340705, green: 0.988355577, blue: 0.4726552367, alpha: 1)))
190+
\("foregroundColor", .foreground(#colorLiteral(red: 0.9995340705, green: 0.988355577, blue: 0.4726552367, alpha: 1)))
191191
192-
\("foregroundColor", .color(#colorLiteral(red: 0.8321695924, green: 0.985483706, blue: 0.4733308554, alpha: 1)))
192+
\("foregroundColor", .foreground(#colorLiteral(red: 0.8321695924, green: 0.985483706, blue: 0.4733308554, alpha: 1)))
193193
194-
\("foregroundColor", .color(#colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1)))
194+
\("foregroundColor", .foreground(#colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1)))
195195
196-
\("foregroundColor", .color(#colorLiteral(red: 0.4508578777, green: 0.9882974029, blue: 0.8376303315, alpha: 1)))
196+
\("foregroundColor", .foreground(#colorLiteral(red: 0.4508578777, green: 0.9882974029, blue: 0.8376303315, alpha: 1)))
197197
198-
\("foregroundColor", .color(#colorLiteral(red: 0.4513868093, green: 0.9930960536, blue: 1, alpha: 1)))
198+
\("foregroundColor", .foreground(#colorLiteral(red: 0.4513868093, green: 0.9930960536, blue: 1, alpha: 1)))
199199
200-
\("foregroundColor", .color(#colorLiteral(red: 0.4620226622, green: 0.8382837176, blue: 1, alpha: 1)))
200+
\("foregroundColor", .foreground(#colorLiteral(red: 0.4620226622, green: 0.8382837176, blue: 1, alpha: 1)))
201201
202-
\("foregroundColor", .color(#colorLiteral(red: 0.476841867, green: 0.5048075914, blue: 1, alpha: 1)))
202+
\("foregroundColor", .foreground(#colorLiteral(red: 0.476841867, green: 0.5048075914, blue: 1, alpha: 1)))
203203
204-
\("foregroundColor", .color(#colorLiteral(red: 0.8446564078, green: 0.5145705342, blue: 1, alpha: 1)))
204+
\("foregroundColor", .foreground(#colorLiteral(red: 0.8446564078, green: 0.5145705342, blue: 1, alpha: 1)))
205205
206-
\("foregroundColor", .color(#colorLiteral(red: 1, green: 0.5212053061, blue: 1, alpha: 1)))
206+
\("foregroundColor", .foreground(#colorLiteral(red: 1, green: 0.5212053061, blue: 1, alpha: 1)))
207207
208-
\("foregroundColor", .color(#colorLiteral(red: 1, green: 0.5409764051, blue: 0.8473142982, alpha: 1)))
208+
\("foregroundColor", .foreground(#colorLiteral(red: 1, green: 0.5409764051, blue: 0.8473142982, alpha: 1)))
209209
210210
""",
211211
code: #"""
@@ -857,7 +857,7 @@ extension AllTableViewController {
857857
\("verticalGlyphForm: 0", .verticalGlyphForm(false))
858858
859859
860-
\("Currently on iOS, it's always horizontal.", .color(.lightGray))
860+
\("Currently on iOS, it's always horizontal.", .foreground(.lightGray))
861861
862862
""",
863863
code: #"""

Demo-TV/Demo-TV/ViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ViewController: UIViewController {
7575
""",
7676
"""
7777
\("Features:", .font(.systemFont(ofSize: 30, weight: .semibold)))
78-
\("foregroundColor", .color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)))
78+
\("foregroundColor", .foreground(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)))
7979
\("backgroundColor", .background(#colorLiteral(red: 0.6642242074, green: 0.6642400622, blue: 0.6642315388, alpha: 1)))
8080
\("font", .font(.systemFont(ofSize: 18, weight: .semibold)))
8181
\("link", .link("https://www.apple.com/"))
@@ -105,11 +105,11 @@ class ViewController: UIViewController {
105105
Embedding
106106
107107
fontSize: 16
108-
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .color(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
108+
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .foreground(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
109109
This is attributed text -> \("underline: single", .underline(.single))
110110
\(wrap: .embedding(
111111
"Test wrap color red \("fontSize: 40 medium", .font(.systemFont(ofSize: 40, weight: .medium)))"
112-
), .font(.systemFont(ofSize: 20)), .color(.red))
112+
), .font(.systemFont(ofSize: 20)), .foreground(.red))
113113
"""
114114
), .font(.systemFont(ofSize: 16))
115115
)
@@ -119,11 +119,11 @@ class ViewController: UIViewController {
119119
Override
120120
121121
fontSize: 16
122-
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .color(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
122+
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .foreground(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
123123
This is attributed text -> \("underline: single", .underline(.single))
124124
\(wrap: .override(
125125
"Test wrap color red \("fontSize: 40 medium", .font(.systemFont(ofSize: 40, weight: .medium)))"
126-
), .font(.systemFont(ofSize: 20)), .color(.red))
126+
), .font(.systemFont(ofSize: 20)), .foreground(.red))
127127
"""
128128
), .font(.systemFont(ofSize: 16))
129129
)

Demo-Watch/Demo-Watch WatchKit Extension/InterfaceController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class InterfaceController: WKInterfaceController {
4040
""",
4141
"""
4242
\("Features:", .font(.systemFont(ofSize: 30, weight: .semibold)))
43-
\("foregroundColor", .color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)))
43+
\("foregroundColor", .foreground(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)))
4444
\("backgroundColor", .background(#colorLiteral(red: 0.6642242074, green: 0.6642400622, blue: 0.6642315388, alpha: 1)))
4545
\("font", .font(.systemFont(ofSize: 18, weight: .semibold)))
4646
\("link", .link("https://www.apple.com/"))
@@ -70,11 +70,11 @@ class InterfaceController: WKInterfaceController {
7070
Embedding
7171
7272
fontSize: 16
73-
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .color(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
73+
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .foreground(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
7474
This is attributed text -> \("underline: single", .underline(.single))
7575
\(wrap: .embedding(
7676
"Test wrap color red \("fontSize: 40 medium", .font(.systemFont(ofSize: 40, weight: .medium)))"
77-
), .font(.systemFont(ofSize: 20)), .color(.red))
77+
), .font(.systemFont(ofSize: 20)), .foreground(.red))
7878
"""
7979
), .font(.systemFont(ofSize: 16))
8080
)
@@ -84,11 +84,11 @@ class InterfaceController: WKInterfaceController {
8484
Override
8585
8686
fontSize: 16
87-
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .color(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
87+
This is attributed text -> \("fontSize: 30", .font(.systemFont(ofSize: 30)), .foreground(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
8888
This is attributed text -> \("underline: single", .underline(.single))
8989
\(wrap: .override(
9090
"Test wrap color red \("fontSize: 40 medium", .font(.systemFont(ofSize: 40, weight: .medium)))"
91-
), .font(.systemFont(ofSize: 20)), .color(.red))
91+
), .font(.systemFont(ofSize: 20)), .foreground(.red))
9292
"""
9393
), .font(.systemFont(ofSize: 16))
9494
)

Demo/Demo/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@
22102210
<outlet property="textView" destination="Q84-7g-O8D" id="h6G-af-UF0"/>
22112211
</connections>
22122212
</view>
2213-
<navigationItem key="navigationItem" title="Highlighted" id="pJu-pa-9kf">
2213+
<navigationItem key="navigationItem" title="Checking" id="pJu-pa-9kf">
22142214
<barButtonItem key="rightBarButtonItem" title="Change Tint" id="MfC-fc-h1i">
22152215
<connections>
22162216
<action selector="changeTintAction:" destination="QfH-3Z-ORs" id="3eu-vs-ise"/>

Demo/Demo/Details/CheckingViewController.swift

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,46 @@ class CheckingViewController: ViewController<CheckingView> {
1414
override func viewDidLoad() {
1515
super.viewDidLoad()
1616
// 添加电话号码类型监听
17-
container.label.attributed.observe([.action], highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))]) { (result) in
17+
container.label.attributed.observe([.phoneNumber], highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))]) { (result) in
1818
print(result)
1919
}
20-
// 添加全部类型监听
21-
container.textView.attributed.observe(highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))]) { (result) in
20+
// 添加默认类型监听
21+
container.textView.attributed.observe(highlights: [.foreground(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))]) { (result) in
2222
print(result)
2323
}
2424
// 移除监听
2525
//container.textView.attributed.remove(checking: .link)
2626

27+
func clicked(_ result: AttributedString.Action.Result) {
28+
switch result.content {
29+
case .string(let value):
30+
print("点击了文本: \n\(value) \nrange: \(result.range)")
31+
32+
case .attachment(let value):
33+
print("点击了附件: \n\(value) \nrange: \(result.range)")
34+
}
35+
}
36+
2737
do {
28-
var string: AttributedString = "我的名字叫李响,我的手机号码是\(18611401994, .action { print("aa") }),我的电子邮件地址是[email protected],现在是2020/06/28 20:30。我的GitHub主页是https://github.com/lixiang1994。"
29-
string.add(attributes: [.foreground(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.address])
30-
string.add(attributes: [.foreground(#colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.link])
38+
var string: AttributedString = """
39+
我的名字叫李响,我的手机号码是18611401994,我的电子邮件地址是[email protected],现在是2020/06/28 20:30。我的GitHub主页是https://github.com/lixiang1994。欢迎来Star! \("点击联系我", .action(clicked))
40+
"""
41+
string.add(attributes: [.foreground(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.phoneNumber])
42+
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.link])
3143
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.date])
44+
string.add(attributes: [.font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.action])
3245
container.label.attributed.text = string
3346
}
3447

3548
do {
36-
var string: AttributedString = "My name is Li Xiang, my mobile phone number is 18611401994, my email address is [email protected], I live in No.10 Xitucheng Road, Haidian District, Beijing, China, and it is now 20:30 on June 28, 2020. My GitHub homepage is https://github.com/lixiang1994."
49+
var string: AttributedString = """
50+
My name is Li Xiang, my mobile phone number is 18611401994, my email address is [email protected], I live in No.10 Xitucheng Road, Haidian District, Beijing, China, and it is now 20:30 on June 28, 2020. My GitHub homepage is https://github.com/lixiang1994. Welcome to star me! \("Contact me", .action(clicked))
51+
"""
3752
string.add(attributes: [.foreground(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1))], checkings: [.address])
38-
string.add(attributes: [.foreground(#colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1))], checkings: [.link])
53+
string.add(attributes: [.foreground(#colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1))], checkings: [.link, .phoneNumber])
3954
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1))], checkings: [.date])
40-
string.add(attributes: [.foreground(#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1))], checkings: [.regex(".a."), .regex(".b.")])
55+
string.add(attributes: [.foreground(#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1))], checkings: [.regex("Li Xiang")])
56+
string.add(attributes: [.font(.systemFont(ofSize: 16, weight: .medium))], checkings: [.action])
4157
container.textView.attributed.text = string
4258
}
4359

@@ -58,7 +74,7 @@ class CheckingView: UIView {
5874
super.tintColorDidChange()
5975
let isDimmed = tintAdjustmentMode == .dimmed
6076
let color = isDimmed ? #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) : #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
61-
label.attributed.text?.add(attributes: [.foreground(color)], checkings: [.phoneNumber])
62-
textView.attributed.text.add(attributes: [.foreground(color)], checkings: [.phoneNumber])
77+
label.attributed.text?.add(attributes: [.foreground(color)], checkings: [.action])
78+
textView.attributed.text.add(attributes: [.foreground(color)], checkings: [.action])
6379
}
6480
}

Sources/AttributedString.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ public struct AttributedString {
2929

3030
public var length: Int {
3131
value.length
32-
3332
}
3433

3534
/// String
3635

37-
public init(_ value: String) {
38-
self.value = .init(string: value)
36+
public init(string value: String, _ attributes: Attribute...) {
37+
self.value = AttributedString(string: value, with: attributes).value
38+
}
39+
40+
public init(string value: String, with attributes: [Attribute] = []) {
41+
self.value = AttributedString(.init(string: value), with: attributes).value
3942
}
4043

4144
/// NSAttributedString
@@ -44,8 +47,6 @@ public struct AttributedString {
4447
self.value = value
4548
}
4649

47-
/// NSAttributedString + Attributes
48-
4950
public init(_ value: NSAttributedString, _ attributes: Attribute...) {
5051
self.value = AttributedString(value, with: attributes).value
5152
}
@@ -64,7 +65,7 @@ public struct AttributedString {
6465
self.value = AttributedString(.init(value), with: attributes).value
6566
}
6667

67-
/// AttributedString + Attributes
68+
/// AttributedString
6869

6970
public init(_ string: AttributedString, _ attributes: Attribute...) {
7071
self.value = AttributedString(wrap: .embedding(string), with: attributes).value

Sources/Checking.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,25 @@ extension AttributedString {
101101

102102
value = string
103103
}
104+
105+
public mutating func set(attributes: [Attribute], checkings: [Checking] = .defalut) {
106+
var temp: [NSAttributedString.Key: Any] = [:]
107+
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
108+
109+
let matched = matching(checkings)
110+
111+
let string = NSMutableAttributedString(attributedString: value)
112+
matched.forEach {
113+
string.setAttributes(temp, range: $0.0)
114+
}
115+
116+
value = string
117+
}
104118
}
105119

106120
extension AttributedString {
107121

108-
/// 匹配检查
122+
/// 匹配检查 (Range 不会出现覆盖情况, 优先级 action > regex > other)
109123
/// - Parameter checkings: 检查类型
110124
/// - Returns: 匹配结果 (范围, 检查类型, 检查结果)
111125
func matching(_ checkings: [Checking]) -> [NSRange: (Checking, Checking.Result)] {
@@ -120,16 +134,16 @@ extension AttributedString {
120134
guard !result.keys.isEmpty else {
121135
return false
122136
}
123-
guard result[range] == nil else {
137+
guard result[range] != nil else {
124138
return false
125139
}
126140
return result.keys.contains(where: { $0.overlap(range) })
127141
}
128142

129143
// Actions
130144
#if os(iOS) || os(macOS)
131-
let actions: [NSRange: AttributedString.Action] = value.get(.action)
132145
if checkings.contains(.action) {
146+
let actions: [NSRange: AttributedString.Action] = value.get(.action)
133147
for action in actions where !contains(action.key) {
134148
result[action.key] = (.action, .action(value.get(action.key)))
135149
}

0 commit comments

Comments
 (0)