-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighlightExtraSymbolsTextStorage.m
104 lines (80 loc) · 2.98 KB
/
HighlightExtraSymbolsTextStorage.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
//
// TextStorage.m
// TextStorageTest
//
// Created by Maxim Smirnov on 29/04/15.
// Copyright (c) 2015 Maxim Smirnov. All rights reserved.
//
#import "HighlightExtraSymbolsTextStorage.h"
@interface HighlightExtraSymbolsTextStorage ()
{
NSMutableAttributedString *_backingStore;
NSUInteger _maxLength;
}
@end
@implementation HighlightExtraSymbolsTextStorage
- (instancetype)init
{
return [self initWithMaximumTextLength:5000];
}
- (instancetype)initWithMaximumTextLength:(NSUInteger)maxLength
{
return [self initWithMaximumTextLength:maxLength text:@""];
}
- (instancetype)initWithMaximumTextLength:(NSUInteger)maxLength text:(NSString *)text
{
self = [super init];
if (self) {
_normalAttributes = @{ NSBackgroundColorAttributeName : [UIColor whiteColor],
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:16.0f] };
_highlightAttributes = @{ NSBackgroundColorAttributeName : [UIColor colorWithRed:1 green:0.921 blue:0.901 alpha:1],
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:16.0f] };
_backingStore = [[NSMutableAttributedString alloc] initWithString:text attributes:_normalAttributes];
_maxLength = maxLength;
[self applyAttributes];
}
return self;
}
- (NSString *)string
{
return [_backingStore string];
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
return [_backingStore attributesAtIndex:location
effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
NSLog(@"replaceCharactersInRange:%@ withString:%@", NSStringFromRange(range), str);
[self beginEditing];
[_backingStore replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters | NSTextStorageEditedAttributes
range:range changeInLength:str.length - range.length];
[self endEditing];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
NSLog(@"setAttributes:%@ range:%@", attrs, NSStringFromRange(range));
[self beginEditing];
[_backingStore setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
[self endEditing];
}
- (void)processEditing
{
[self applyAttributes];
[super processEditing];
}
- (void)applyAttributes
{
[_backingStore setAttributes:self.normalAttributes
range:NSMakeRange(0, _backingStore.length)];
if (_backingStore.length > _maxLength) {
[_backingStore setAttributes:self.highlightAttributes
range:NSMakeRange(_maxLength, _backingStore.length - _maxLength)];
}
}
@end