forked from lixiang1994/AttributedString
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributedString.swift
248 lines (198 loc) · 8.21 KB
/
AttributedString.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// AttributedString.swift
// ┌─┐ ┌───────┐ ┌───────┐
// │ │ │ ┌─────┘ │ ┌─────┘
// │ │ │ └─────┐ │ └─────┐
// │ │ │ ┌─────┘ │ ┌─────┘
// │ └─────┐│ └─────┐ │ └─────┐
// └───────┘└───────┘ └───────┘
//
// Created by Lee on 2019/11/18.
// Copyright © 2019 LEE. All rights reserved.
//
#if os(macOS)
import AppKit
public typealias ASImage = NSImage
public typealias ASColor = NSColor
public typealias ASFont = NSFont
#else
import UIKit
public typealias ASImage = UIImage
public typealias ASColor = UIColor
public typealias ASFont = UIFont
#endif
public struct ASAttributedString {
internal init(value: NSAttributedString) {
self.value = value
}
public internal(set) var value: NSAttributedString
public var length: Int {
value.length
}
/// String
public init(string value: String, _ attributes: Attribute...) {
self.value = ASAttributedString(string: value, with: attributes).value
}
public init(string value: String, with attributes: [Attribute] = []) {
self.value = ASAttributedString(.init(string: value), with: attributes).value
}
/// NSAttributedString
public init(_ value: NSAttributedString) {
self.value = value
}
public init(_ value: NSAttributedString, _ attributes: Attribute...) {
self.value = ASAttributedString(value, with: attributes).value
}
public init?(_ value: NSAttributedString?, _ attributes: Attribute...) {
guard let value = value else { return nil }
self.value = ASAttributedString(value, with: attributes).value
}
public init(_ value: NSAttributedString, with attributes: [Attribute]) {
self.value = ASAttributedString(.init(value), with: attributes).value
}
public init?(_ value: NSAttributedString?, with attributes: [Attribute] = []) {
guard let value = value else { return nil }
self.value = ASAttributedString(.init(value), with: attributes).value
}
/// AttributedString
public init(_ string: ASAttributedString, _ attributes: Attribute...) {
self.value = ASAttributedString(wrap: .embedding(string), with: attributes).value
}
public init(_ string: ASAttributedString, with attributes: [Attribute] = []) {
self.value = ASAttributedString(wrap: .embedding(string), with: attributes).value
}
public init(wrap mode: WrapMode, _ attributes: Attribute...) {
self.value = ASAttributedString(wrap: mode, with: attributes).value
}
public init(wrap mode: WrapMode, with attributes: [Attribute]) {
guard !attributes.isEmpty else {
self.value = mode.value.value
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
switch mode {
case .embedding(let value):
string = .init(attributedString: value.value)
// 过滤后的属性以及范围
var ranges: [([NSAttributedString.Key: Any], NSRange)] = []
// 遍历原属性 去除重复属性 防止覆盖
string.enumerateAttributes(
in: .init(location: 0, length: string.length),
options: .longestEffectiveRangeNotRequired
) { (attributs, range, stop) in
// 差集 从通用属性中过滤掉原本就存在的属性
let keys = Set(temp.keys).subtracting(Set(attributs.keys))
ranges.append((temp.filter { keys.contains($0.key) }, range))
}
// 添加过滤后的属性和相应的范围
ranges.forEach { string.addAttributes($0, range: $1) }
case .override(let value):
string = .init(attributedString: value.value)
string.addAttributes(temp, range: .init(location: 0, length: string.length))
}
self.value = string
}
}
extension ASAttributedString: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.value = .init(string: value)
}
}
extension ASAttributedString: CustomStringConvertible {
public var description: String {
.init(describing: value)
}
}
extension ASAttributedString: Equatable {
public static func == (lhs: ASAttributedString, rhs: ASAttributedString) -> Bool {
guard lhs.length == rhs.length else {
return false
}
guard lhs.value.string == rhs.value.string else {
return false
}
guard lhs.value.get(.init(location: 0, length: lhs.length)) == rhs.value.get(.init(location: 0, length: rhs.length)) else {
return false
}
return true
}
/// 内容是否相等
/// - Parameter other: 其他AttributedString
/// - Returns: 结果
public func isContentEqual(to other: ASAttributedString?) -> Bool {
guard let other = other else {
return false
}
guard length == other.length else {
return false
}
return value.string == other.value.string
}
}
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)
string.addAttributes(temp, range: range)
value = string
}
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)
string.setAttributes(temp, range: range)
value = string
}
}
extension ASAttributedString {
public func add(attributes: Attribute..., range: NSRange? = .none) -> Self {
return add(attributes, range: range ?? .init(location: 0, length: length))
}
public func add(_ attributes: [Attribute], range: NSRange) -> Self {
var temp = self
temp.add(attributes: attributes, range: range)
return temp
}
public func set(attributes: Attribute..., range: NSRange? = .none) -> Self {
return set(attributes, range: range ?? .init(location: 0, length: length))
}
public func set(_ attributes: [Attribute], range: NSRange) -> Self {
var temp = self
temp.set(attributes: attributes, range: range)
return temp
}
}
fileprivate extension Dictionary where Key == NSAttributedString.Key, Value == Any {
static func == (lhs: [NSAttributedString.Key: Any], rhs: [NSAttributedString.Key: Any]) -> Bool {
lhs.keys == rhs.keys ? NSDictionary(dictionary: lhs).isEqual(to: rhs) : false
}
}
fileprivate extension Dictionary where Key == NSRange, Value == [NSAttributedString.Key: Any] {
static func == (lhs: [NSRange: [NSAttributedString.Key: Any]], rhs: [NSRange: [NSAttributedString.Key: Any]]) -> Bool {
guard lhs.count == rhs.count else {
return false
}
return zip(lhs, rhs).allSatisfy { (l, r) -> Bool in
l.0 == r.0 && l.1 == r.1
}
}
}