forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTextAttribute.mm
More file actions
487 lines (409 loc) · 16.1 KB
/
Copy pathASTextAttribute.mm
File metadata and controls
487 lines (409 loc) · 16.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//
// ASTextAttribute.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASTextAttribute.h"
#import <CoreText/CoreText.h>
#import <AsyncDisplayKit/NSAttributedString+ASText.h>
NSString *const ASTextBackedStringAttributeName = @"ASTextBackedString";
NSString *const ASTextBindingAttributeName = @"ASTextBinding";
NSString *const ASTextShadowAttributeName = @"ASTextShadow";
NSString *const ASTextInnerShadowAttributeName = @"ASTextInnerShadow";
NSString *const ASTextUnderlineAttributeName = @"ASTextUnderline";
NSString *const ASTextStrikethroughAttributeName = @"ASTextStrikethrough";
NSString *const ASTextBorderAttributeName = @"ASTextBorder";
NSString *const ASTextBackgroundBorderAttributeName = @"ASTextBackgroundBorder";
NSString *const ASTextBlockBorderAttributeName = @"ASTextBlockBorder";
NSString *const ASTextAttachmentAttributeName = @"ASTextAttachment";
NSString *const ASTextHighlightAttributeName = @"ASTextHighlight";
NSString *const ASTextGlyphTransformAttributeName = @"ASTextGlyphTransform";
NSString *const ASTextAttachmentToken = @"\uFFFC";
NSString *const ASTextTruncationToken = @"\u2026";
ASTextAttributeType ASTextAttributeGetType(NSString *name){
if (name.length == 0) return ASTextAttributeTypeNone;
static NSMutableDictionary *dic;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dic = [NSMutableDictionary new];
NSNumber *All = @(ASTextAttributeTypeUIKit | ASTextAttributeTypeCoreText | ASTextAttributeTypeASText);
NSNumber *CoreText_ASText = @(ASTextAttributeTypeCoreText | ASTextAttributeTypeASText);
NSNumber *UIKit_ASText = @(ASTextAttributeTypeUIKit | ASTextAttributeTypeASText);
NSNumber *UIKit_CoreText = @(ASTextAttributeTypeUIKit | ASTextAttributeTypeCoreText);
NSNumber *UIKit = @(ASTextAttributeTypeUIKit);
NSNumber *CoreText = @(ASTextAttributeTypeCoreText);
NSNumber *ASText = @(ASTextAttributeTypeASText);
dic[NSFontAttributeName] = All;
dic[NSKernAttributeName] = All;
dic[NSForegroundColorAttributeName] = UIKit;
dic[(id)kCTForegroundColorAttributeName] = CoreText;
dic[(id)kCTForegroundColorFromContextAttributeName] = CoreText;
dic[NSBackgroundColorAttributeName] = UIKit;
dic[NSStrokeWidthAttributeName] = All;
dic[NSStrokeColorAttributeName] = UIKit;
dic[(id)kCTStrokeColorAttributeName] = CoreText_ASText;
dic[NSShadowAttributeName] = UIKit_ASText;
dic[NSStrikethroughStyleAttributeName] = UIKit;
dic[NSUnderlineStyleAttributeName] = UIKit_CoreText;
dic[(id)kCTUnderlineColorAttributeName] = CoreText;
dic[NSLigatureAttributeName] = All;
dic[(id)kCTSuperscriptAttributeName] = UIKit; //it's a CoreText attrubite, but only supported by UIKit...
dic[NSVerticalGlyphFormAttributeName] = All;
dic[(id)kCTGlyphInfoAttributeName] = CoreText_ASText;
#if TARGET_OS_IOS
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
dic[(id)kCTCharacterShapeAttributeName] = CoreText_ASText;
#pragma clang diagnostic pop
#endif
dic[(id)kCTRunDelegateAttributeName] = CoreText_ASText;
dic[(id)kCTBaselineClassAttributeName] = CoreText_ASText;
dic[(id)kCTBaselineInfoAttributeName] = CoreText_ASText;
dic[(id)kCTBaselineReferenceInfoAttributeName] = CoreText_ASText;
dic[(id)kCTWritingDirectionAttributeName] = CoreText_ASText;
dic[NSParagraphStyleAttributeName] = All;
dic[NSStrikethroughColorAttributeName] = UIKit;
dic[NSUnderlineColorAttributeName] = UIKit;
dic[NSTextEffectAttributeName] = UIKit;
dic[NSObliquenessAttributeName] = UIKit;
dic[NSExpansionAttributeName] = UIKit;
dic[(id)kCTLanguageAttributeName] = CoreText_ASText;
dic[NSBaselineOffsetAttributeName] = UIKit;
dic[NSWritingDirectionAttributeName] = All;
dic[NSAttachmentAttributeName] = UIKit;
dic[NSLinkAttributeName] = UIKit;
dic[(id)kCTRubyAnnotationAttributeName] = CoreText;
dic[ASTextBackedStringAttributeName] = ASText;
dic[ASTextBindingAttributeName] = ASText;
dic[ASTextShadowAttributeName] = ASText;
dic[ASTextInnerShadowAttributeName] = ASText;
dic[ASTextUnderlineAttributeName] = ASText;
dic[ASTextStrikethroughAttributeName] = ASText;
dic[ASTextBorderAttributeName] = ASText;
dic[ASTextBackgroundBorderAttributeName] = ASText;
dic[ASTextBlockBorderAttributeName] = ASText;
dic[ASTextAttachmentAttributeName] = ASText;
dic[ASTextHighlightAttributeName] = ASText;
dic[ASTextGlyphTransformAttributeName] = ASText;
});
NSNumber *num = dic[name];
if (num) return num.integerValue;
return ASTextAttributeTypeNone;
}
@implementation ASTextBackedString
+ (instancetype)stringWithString:(NSString *)string NS_RETURNS_RETAINED {
ASTextBackedString *one = [self new];
one.string = string;
return one;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.string forKey:@"string"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
_string = [aDecoder decodeObjectForKey:@"string"];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
one.string = self.string;
return one;
}
@end
@implementation ASTextBinding
+ (instancetype)bindingWithDeleteConfirm:(BOOL)deleteConfirm NS_RETURNS_RETAINED {
ASTextBinding *one = [self new];
one.deleteConfirm = deleteConfirm;
return one;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:@(self.deleteConfirm) forKey:@"deleteConfirm"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
_deleteConfirm = ((NSNumber *)[aDecoder decodeObjectForKey:@"deleteConfirm"]).boolValue;
return self;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
one.deleteConfirm = self.deleteConfirm;
return one;
}
@end
@implementation ASTextShadow
+ (instancetype)shadowWithColor:(UIColor *)color offset:(CGSize)offset radius:(CGFloat)radius NS_RETURNS_RETAINED {
ASTextShadow *one = [self new];
one.color = color;
one.offset = offset;
one.radius = radius;
return one;
}
+ (instancetype)shadowWithNSShadow:(NSShadow *)nsShadow NS_RETURNS_RETAINED {
if (!nsShadow) return nil;
ASTextShadow *shadow = [self new];
shadow.offset = nsShadow.shadowOffset;
shadow.radius = nsShadow.shadowBlurRadius;
id color = nsShadow.shadowColor;
if (color) {
if (CGColorGetTypeID() == CFGetTypeID((__bridge CFTypeRef)(color))) {
color = [UIColor colorWithCGColor:(__bridge CGColorRef)(color)];
}
if ([color isKindOfClass:[UIColor class]]) {
shadow.color = color;
}
}
return shadow;
}
- (NSShadow *)nsShadow {
NSShadow *shadow = [NSShadow new];
shadow.shadowOffset = self.offset;
shadow.shadowBlurRadius = self.radius;
shadow.shadowColor = self.color;
return shadow;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.color forKey:@"color"];
[aCoder encodeObject:@(self.radius) forKey:@"radius"];
[aCoder encodeObject:[NSValue valueWithCGSize:self.offset] forKey:@"offset"];
[aCoder encodeObject:self.subShadow forKey:@"subShadow"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
_color = [aDecoder decodeObjectForKey:@"color"];
_radius = ((NSNumber *)[aDecoder decodeObjectForKey:@"radius"]).floatValue;
_offset = ((NSValue *)[aDecoder decodeObjectForKey:@"offset"]).CGSizeValue;
_subShadow = [aDecoder decodeObjectForKey:@"subShadow"];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
one.color = self.color;
one.radius = self.radius;
one.offset = self.offset;
one.subShadow = self.subShadow.copy;
return one;
}
@end
@implementation ASTextDecoration
- (instancetype)init {
self = [super init];
_style = ASTextLineStyleSingle;
return self;
}
+ (instancetype)decorationWithStyle:(ASTextLineStyle)style NS_RETURNS_RETAINED {
ASTextDecoration *one = [self new];
one.style = style;
return one;
}
+ (instancetype)decorationWithStyle:(ASTextLineStyle)style width:(NSNumber *)width color:(UIColor *)color NS_RETURNS_RETAINED {
ASTextDecoration *one = [self new];
one.style = style;
one.width = width;
one.color = color;
return one;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:@(self.style) forKey:@"style"];
[aCoder encodeObject:self.width forKey:@"width"];
[aCoder encodeObject:self.color forKey:@"color"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
self.style = ((NSNumber *)[aDecoder decodeObjectForKey:@"style"]).unsignedIntegerValue;
self.width = [aDecoder decodeObjectForKey:@"width"];
self.color = [aDecoder decodeObjectForKey:@"color"];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
one.style = self.style;
one.width = self.width;
one.color = self.color;
return one;
}
@end
@implementation ASTextBorder
+ (instancetype)borderWithLineStyle:(ASTextLineStyle)lineStyle lineWidth:(CGFloat)width strokeColor:(UIColor *)color NS_RETURNS_RETAINED {
ASTextBorder *one = [self new];
one.lineStyle = lineStyle;
one.strokeWidth = width;
one.strokeColor = color;
return one;
}
+ (instancetype)borderWithFillColor:(UIColor *)color cornerRadius:(CGFloat)cornerRadius NS_RETURNS_RETAINED {
ASTextBorder *one = [self new];
one.fillColor = color;
one.cornerRadius = cornerRadius;
one.insets = UIEdgeInsetsMake(-2, 0, 0, -2);
return one;
}
- (instancetype)init {
self = [super init];
self.lineStyle = ASTextLineStyleSingle;
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:@(self.lineStyle) forKey:@"lineStyle"];
[aCoder encodeObject:@(self.strokeWidth) forKey:@"strokeWidth"];
[aCoder encodeObject:self.strokeColor forKey:@"strokeColor"];
[aCoder encodeObject:@(self.lineJoin) forKey:@"lineJoin"];
[aCoder encodeObject:[NSValue valueWithUIEdgeInsets:self.insets] forKey:@"insets"];
[aCoder encodeObject:@(self.cornerRadius) forKey:@"cornerRadius"];
[aCoder encodeObject:self.shadow forKey:@"shadow"];
[aCoder encodeObject:self.fillColor forKey:@"fillColor"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
_lineStyle = ((NSNumber *)[aDecoder decodeObjectForKey:@"lineStyle"]).unsignedIntegerValue;
_strokeWidth = ((NSNumber *)[aDecoder decodeObjectForKey:@"strokeWidth"]).doubleValue;
_strokeColor = [aDecoder decodeObjectForKey:@"strokeColor"];
_lineJoin = (CGLineJoin)((NSNumber *)[aDecoder decodeObjectForKey:@"join"]).unsignedIntegerValue;
_insets = ((NSValue *)[aDecoder decodeObjectForKey:@"insets"]).UIEdgeInsetsValue;
_cornerRadius = ((NSNumber *)[aDecoder decodeObjectForKey:@"cornerRadius"]).doubleValue;
_shadow = [aDecoder decodeObjectForKey:@"shadow"];
_fillColor = [aDecoder decodeObjectForKey:@"fillColor"];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
one.lineStyle = self.lineStyle;
one.strokeWidth = self.strokeWidth;
one.strokeColor = self.strokeColor;
one.lineJoin = self.lineJoin;
one.insets = self.insets;
one.cornerRadius = self.cornerRadius;
one.shadow = self.shadow.copy;
one.fillColor = self.fillColor;
return one;
}
@end
@implementation ASTextAttachment
+ (instancetype)attachmentWithContent:(id)content NS_RETURNS_RETAINED {
ASTextAttachment *one = [self new];
one.content = content;
return one;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.content forKey:@"content"];
[aCoder encodeObject:[NSValue valueWithUIEdgeInsets:self.contentInsets] forKey:@"contentInsets"];
[aCoder encodeObject:self.userInfo forKey:@"userInfo"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
_content = [aDecoder decodeObjectForKey:@"content"];
_contentInsets = ((NSValue *)[aDecoder decodeObjectForKey:@"contentInsets"]).UIEdgeInsetsValue;
_userInfo = [aDecoder decodeObjectForKey:@"userInfo"];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
if ([self.content respondsToSelector:@selector(copy)]) {
one.content = [self.content copy];
} else {
one.content = self.content;
}
one.contentInsets = self.contentInsets;
one.userInfo = self.userInfo.copy;
return one;
}
@end
@implementation ASTextHighlight
+ (instancetype)highlightWithAttributes:(NSDictionary *)attributes NS_RETURNS_RETAINED {
ASTextHighlight *one = [self new];
one.attributes = attributes;
return one;
}
+ (instancetype)highlightWithBackgroundColor:(UIColor *)color NS_RETURNS_RETAINED {
ASTextBorder *highlightBorder = [ASTextBorder new];
highlightBorder.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
highlightBorder.cornerRadius = 3;
highlightBorder.fillColor = color;
ASTextHighlight *one = [self new];
[one setBackgroundBorder:highlightBorder];
return one;
}
- (void)setAttributes:(NSDictionary *)attributes {
_attributes = attributes.mutableCopy;
}
- (id)copyWithZone:(NSZone *)zone {
__typeof__(self) one = [self.class new];
one.attributes = self.attributes.mutableCopy;
return one;
}
- (void)_makeMutableAttributes {
if (!_attributes) {
_attributes = [NSMutableDictionary new];
} else if (![_attributes isKindOfClass:[NSMutableDictionary class]]) {
_attributes = _attributes.mutableCopy;
}
}
- (void)setFont:(UIFont *)font {
[self _makeMutableAttributes];
if (font == (id)[NSNull null] || font == nil) {
((NSMutableDictionary *)_attributes)[(id)kCTFontAttributeName] = [NSNull null];
} else {
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
if (ctFont) {
((NSMutableDictionary *)_attributes)[(id)kCTFontAttributeName] = (__bridge id)(ctFont);
CFRelease(ctFont);
}
}
}
- (void)setColor:(UIColor *)color {
[self _makeMutableAttributes];
if (color == (id)[NSNull null] || color == nil) {
((NSMutableDictionary *)_attributes)[(id)kCTForegroundColorAttributeName] = [NSNull null];
((NSMutableDictionary *)_attributes)[NSForegroundColorAttributeName] = [NSNull null];
} else {
((NSMutableDictionary *)_attributes)[(id)kCTForegroundColorAttributeName] = (__bridge id)(color.CGColor);
((NSMutableDictionary *)_attributes)[NSForegroundColorAttributeName] = color;
}
}
- (void)setStrokeWidth:(NSNumber *)width {
[self _makeMutableAttributes];
if (width == (id)[NSNull null] || width == nil) {
((NSMutableDictionary *)_attributes)[(id)kCTStrokeWidthAttributeName] = [NSNull null];
} else {
((NSMutableDictionary *)_attributes)[(id)kCTStrokeWidthAttributeName] = width;
}
}
- (void)setStrokeColor:(UIColor *)color {
[self _makeMutableAttributes];
if (color == (id)[NSNull null] || color == nil) {
((NSMutableDictionary *)_attributes)[(id)kCTStrokeColorAttributeName] = [NSNull null];
((NSMutableDictionary *)_attributes)[NSStrokeColorAttributeName] = [NSNull null];
} else {
((NSMutableDictionary *)_attributes)[(id)kCTStrokeColorAttributeName] = (__bridge id)(color.CGColor);
((NSMutableDictionary *)_attributes)[NSStrokeColorAttributeName] = color;
}
}
- (void)setTextAttribute:(NSString *)attribute value:(id)value {
[self _makeMutableAttributes];
if (value == nil) value = [NSNull null];
((NSMutableDictionary *)_attributes)[attribute] = value;
}
- (void)setShadow:(ASTextShadow *)shadow {
[self setTextAttribute:ASTextShadowAttributeName value:shadow];
}
- (void)setInnerShadow:(ASTextShadow *)shadow {
[self setTextAttribute:ASTextInnerShadowAttributeName value:shadow];
}
- (void)setUnderline:(ASTextDecoration *)underline {
[self setTextAttribute:ASTextUnderlineAttributeName value:underline];
}
- (void)setStrikethrough:(ASTextDecoration *)strikethrough {
[self setTextAttribute:ASTextStrikethroughAttributeName value:strikethrough];
}
- (void)setBackgroundBorder:(ASTextBorder *)border {
[self setTextAttribute:ASTextBackgroundBorderAttributeName value:border];
}
- (void)setBorder:(ASTextBorder *)border {
[self setTextAttribute:ASTextBorderAttributeName value:border];
}
- (void)setAttachment:(ASTextAttachment *)attachment {
[self setTextAttribute:ASTextAttachmentAttributeName value:attachment];
}
@end