forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASMutableAttributedStringBuilder.m
More file actions
255 lines (212 loc) · 7 KB
/
Copy pathASMutableAttributedStringBuilder.m
File metadata and controls
255 lines (212 loc) · 7 KB
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
249
250
251
252
253
254
255
//
// ASMutableAttributedStringBuilder.m
// AsyncDisplayKit
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import <AsyncDisplayKit/ASMutableAttributedStringBuilder.h>
@implementation ASMutableAttributedStringBuilder {
// Flag for the type of the current transaction (set or add)
BOOL _setRange;
// The range over which the currently pending transaction will occur
NSRange _pendingRange;
// The actual attribute dictionary that is being composed
NSMutableDictionary *_pendingRangeAttributes;
NSMutableAttributedString *_attrStr;
// We delay initialization of the _attrStr until we need to
NSString *_initString;
}
- (instancetype)init
{
if (self = [super init]) {
_attrStr = [[NSMutableAttributedString alloc] init];
_pendingRange.location = NSNotFound;
}
return self;
}
- (instancetype)initWithString:(NSString *)str
{
return [self initWithString:str attributes:@{}];
}
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary *)attrs
{
if (self = [super init]) {
// We cache this in an ivar that we can lazily construct the attributed
// string with when we get to a forced commit point.
_initString = str;
// Triggers a creation of the _pendingRangeAttributes dictionary which then
// is filled with entries from the given attrs dict.
[[self _pendingRangeAttributes] addEntriesFromDictionary:attrs];
_setRange = NO;
_pendingRange = NSMakeRange(0, _initString.length);
}
return self;
}
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr
{
if (self = [super init]) {
_attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:attrStr];
_pendingRange.location = NSNotFound;
}
return self;
}
- (NSMutableAttributedString *)_attributedString
{
if (_attrStr == nil && _initString != nil) {
// We can lazily construct the attributed string if it hasn't already been
// created with the existing pending attributes. This is significantly
// faster if more attributes are added after initializing this instance
// and the new attributions are for the entire string anyway.
_attrStr = [[NSMutableAttributedString alloc] initWithString:_initString attributes:_pendingRangeAttributes];
_pendingRangeAttributes = nil;
_pendingRange.location = NSNotFound;
_initString = nil;
}
return _attrStr;
}
#pragma mark - Pending attribution
- (NSMutableDictionary *)_pendingRangeAttributes
{
// Lazy dictionary creation. Call this if you want to force initialization,
// otherwise just use the ivar.
if (_pendingRangeAttributes == nil) {
_pendingRangeAttributes = [[NSMutableDictionary alloc] init];
}
return _pendingRangeAttributes;
}
- (void)_applyPendingRangeAttributions
{
if (_attrStr == nil) {
// Trigger its creation if it doesn't exist.
[self _attributedString];
}
if (_pendingRangeAttributes.count == 0) {
return;
}
if (_pendingRange.location == NSNotFound) {
return;
}
if (_setRange) {
[[self _attributedString] setAttributes:_pendingRangeAttributes range:_pendingRange];
} else {
[[self _attributedString] addAttributes:_pendingRangeAttributes range:_pendingRange];
}
_pendingRangeAttributes = nil;
_pendingRange.location = NSNotFound;
}
#pragma mark - Editing
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
[self _applyPendingRangeAttributions];
[[self _attributedString] replaceCharactersInRange:range withString:str];
}
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString
{
[self _applyPendingRangeAttributions];
[[self _attributedString] replaceCharactersInRange:range withAttributedString:attrString];
}
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range
{
if (_setRange) {
[self _applyPendingRangeAttributions];
_setRange = NO;
}
if (!NSEqualRanges(_pendingRange, range)) {
[self _applyPendingRangeAttributions];
_pendingRange = range;
}
NSMutableDictionary *pendingAttributes = [self _pendingRangeAttributes];
pendingAttributes[name] = value;
}
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range
{
if (_setRange) {
[self _applyPendingRangeAttributions];
_setRange = NO;
}
if (!NSEqualRanges(_pendingRange, range)) {
[self _applyPendingRangeAttributions];
_pendingRange = range;
}
NSMutableDictionary *pendingAttributes = [self _pendingRangeAttributes];
[pendingAttributes addEntriesFromDictionary:attrs];
}
- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc
{
[self _applyPendingRangeAttributions];
[[self _attributedString] insertAttributedString:attrString atIndex:loc];
}
- (void)appendAttributedString:(NSAttributedString *)attrString
{
[self _applyPendingRangeAttributions];
[[self _attributedString] appendAttributedString:attrString];
}
- (void)deleteCharactersInRange:(NSRange)range
{
[self _applyPendingRangeAttributions];
[[self _attributedString] deleteCharactersInRange:range];
}
- (void)setAttributedString:(NSAttributedString *)attrString
{
[self _applyPendingRangeAttributions];
[[self _attributedString] setAttributedString:attrString];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
if (!_setRange) {
[self _applyPendingRangeAttributions];
_setRange = YES;
}
if (!NSEqualRanges(_pendingRange, range)) {
[self _applyPendingRangeAttributions];
_pendingRange = range;
}
NSMutableDictionary *pendingAttributes = [self _pendingRangeAttributes];
[pendingAttributes addEntriesFromDictionary:attrs];
}
- (void)removeAttribute:(NSString *)name range:(NSRange)range
{
// This call looks like the other set/add functions, but in order for this
// function to perform as advertised we MUST first add the attributes we
// currently have pending.
[self _applyPendingRangeAttributions];
[[self _attributedString] removeAttribute:name range:range];
}
#pragma mark - Output
- (NSMutableAttributedString *)composedAttributedString
{
if (_pendingRangeAttributes.count > 0) {
[self _applyPendingRangeAttributions];
}
return [self _attributedString];
}
#pragma mark - Forwarding
- (NSUInteger)length
{
// If we just want a length call, no need to lazily construct the attributed string
return _attrStr ? _attrStr.length : _initString.length;
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
return [[self _attributedString] attributesAtIndex:location effectiveRange:range];
}
- (NSString *)string
{
return _attrStr ? _attrStr.string : _initString;
}
- (NSMutableString *)mutableString
{
return [[self _attributedString] mutableString];
}
- (void)beginEditing
{
[[self _attributedString] beginEditing];
}
- (void)endEditing
{
[[self _attributedString] endEditing];
}
@end