forked from Skittyblock/FilzaPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSTextStorage.m
112 lines (82 loc) · 3.52 KB
/
SSTextStorage.m
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
// SSTextStorage.m
#import "SSTextStorage.h"
#import "SSCodeString.h"
#import "Tweak.h"
@implementation SSTextStorage {
NSMutableAttributedString *_cache;
}
- (id)init {
self = [super init];
if (self) {
_cache = [NSMutableAttributedString new];
}
return self;
}
- (void)setContent:(SSCodeString *)content {
_content = content;
[self beginEditing];
NSInteger oldLength = _cache.length;
[_cache replaceCharactersInRange:NSMakeRange(0, oldLength) withString:_content];
[self edited:NSTextStorageEditedCharacters range:NSMakeRange(0, oldLength) changeInLength:(NSInteger)_content.length - oldLength];
[self updateAttributesForChangedRange: NSMakeRange(0, _content.length)];
[self endEditing];
}
- (void)setFont:(UIFont *)font {
_font = font;
[self beginEditing];
[self updateAttributesForChangedRange: NSMakeRange(0, self.content.length)];
[self endEditing];
}
- (NSString *)string {
return _cache.string;
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range {
return [_cache attributesAtIndex:location effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str {
[self.content replaceCharactersInRange:range withString:str];
[_cache replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters range:range changeInLength:(NSInteger)str.length - (NSInteger)range.length];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range {
[_cache setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}
- (void)processEditing {
[self updateAttributesForChangedRange: self.editedRange];
[super processEditing];
}
- (UIColor *)textColorForCodeType:(SSCodeType)type {
switch (type) {
default:
case SSCodeTypeText:
return [[NSClassFromString(@"ThemeManager") sharedInstance] text];
case SSCodeTypeKeyword:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"keywords"]);
case SSCodeTypeClass:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"classes"]);
case SSCodeTypePragma:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"preprocessors"]);
case SSCodeTypeNumber:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"numbers"]);
case SSCodeTypeURL:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"urls"]);
case SSCodeTypeAttribute:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"attributes"]);
case SSCodeTypeString:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"strings"]);
case SSCodeTypeComment:
return colorFromHex([[NSClassFromString(@"ThemeManager") sharedInstance] syntaxTheme][@"components"][@"comments"]);
}
}
- (void)updateAttributesForChangedRange:(NSRange)range {
range = [self.content paragraphRangeForRange:range];
[self setAttributes:@{} range:range];
if (self.font) {
[self addAttribute:NSFontAttributeName value:self.font range:range];
}
[self.content enumerateCodeInRange:range usingBlock:^(NSRange range, SSCodeType type) {
[self addAttribute:NSForegroundColorAttributeName value:[self textColorForCodeType: type] range:range];
}];
}
@end