This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathSLKTextView+SLKAdditions.m
185 lines (144 loc) · 6.13 KB
/
SLKTextView+SLKAdditions.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
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
//
// SlackTextViewController
// https://github.com/slackhq/SlackTextViewController
//
// Copyright 2014-2016 Slack Technologies, Inc.
// Licence: MIT-Licence
//
#import "SLKTextView+SLKAdditions.h"
@implementation SLKTextView (SLKAdditions)
- (void)slk_clearText:(BOOL)clearUndo
{
// Important to call self implementation, as SLKTextView overrides setText: to add additional features.
[self setAttributedText:nil];
if (self.undoManagerEnabled && clearUndo) {
[self.undoManager removeAllActions];
}
}
- (void)slk_scrollToCaretPositonAnimated:(BOOL)animated
{
if (animated) {
[self scrollRangeToVisible:self.selectedRange];
}
else {
[UIView performWithoutAnimation:^{
[self scrollRangeToVisible:self.selectedRange];
}];
}
}
- (void)slk_scrollToBottomAnimated:(BOOL)animated
{
CGRect rect = [self caretRectForPosition:self.selectedTextRange.end];
rect.size.height += self.textContainerInset.bottom;
if (animated) {
[self scrollRectToVisible:rect animated:animated];
}
else {
[UIView performWithoutAnimation:^{
[self scrollRectToVisible:rect animated:NO];
}];
}
}
- (void)slk_insertNewLineBreak
{
[self slk_insertTextAtCaretRange:@"\n"];
// if the text view cannot expand anymore, scrolling to bottom are not animated to fix a UITextView issue scrolling twice.
BOOL animated = !self.isExpanding;
//Detected break. Should scroll to bottom if needed.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0125 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self slk_scrollToBottomAnimated:animated];
});
}
- (void)slk_insertTextAtCaretRange:(NSString *)text
{
NSRange range = [self slk_insertText:text inRange:self.selectedRange];
self.selectedRange = NSMakeRange(range.location, 0);
}
- (void)slk_insertTextAtCaretRange:(NSString *)text withAttributes:(NSDictionary<NSString *, id> *)attributes
{
NSRange range = [self slk_insertText:text withAttributes:attributes inRange:self.selectedRange];
self.selectedRange = NSMakeRange(range.location, 0);
}
- (NSRange)slk_insertText:(NSString *)text inRange:(NSRange)range
{
NSAttributedString *attributedText = [self slk_defaultAttributedStringForText:text];
return [self slk_insertAttributedText:attributedText inRange:range];
}
- (NSRange)slk_insertText:(NSString *)text withAttributes:(NSDictionary<NSString *, id> *)attributes inRange:(NSRange)range
{
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
return [self slk_insertAttributedText:attributedText inRange:range];
}
- (NSAttributedString *)slk_setAttributes:(NSDictionary<NSString *, id> *)attributes
inRange:(NSRange)range
{
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
[attributedText setAttributes:attributes range:range];
[self setAttributedText:attributedText];
return self.attributedText;
}
- (void)slk_insertAttributedTextAtCaretRange:(NSAttributedString *)attributedText
{
NSRange range = [self slk_insertAttributedText:attributedText inRange:self.selectedRange];
self.selectedRange = NSMakeRange(range.location, 0);
}
- (NSRange)slk_insertAttributedText:(NSAttributedString *)attributedText inRange:(NSRange)range
{
// Skip if the attributed text is empty
if (attributedText.length == 0) {
return NSMakeRange(0, 0);
}
// Registers for undo management
[self slk_prepareForUndo:@"Attributed text appending"];
// Append the new string at the caret position
if (range.length == 0)
{
NSAttributedString *leftAttributedString = [self.attributedText attributedSubstringFromRange:NSMakeRange(0, range.location)];
NSAttributedString *rightAttributedString = [self.attributedText attributedSubstringFromRange:NSMakeRange(range.location, self.attributedText.length-range.location)];
NSMutableAttributedString *newAttributedText = [NSMutableAttributedString new];
[newAttributedText appendAttributedString:leftAttributedString];
[newAttributedText appendAttributedString:attributedText];
[newAttributedText appendAttributedString:rightAttributedString];
[self setAttributedText:newAttributedText];
range.location += attributedText.length;
return range;
}
// Some text is selected, so we replace it with the new text
else if (range.location != NSNotFound && range.length > 0)
{
NSMutableAttributedString *mutableAttributeText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
[mutableAttributeText replaceCharactersInRange:range withAttributedString:attributedText];
[self setAttributedText:mutableAttributeText];
range.location += self.attributedText.length;
return range;
}
// No text has been inserted, but still return the caret range
return self.selectedRange;
}
- (void)slk_clearAllAttributesInRange:(NSRange)range
{
NSMutableAttributedString *mutableAttributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
[mutableAttributedText setAttributes:nil range:range];
[self setAttributedText:mutableAttributedText];
}
- (NSAttributedString *)slk_defaultAttributedStringForText:(NSString *)text
{
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
if (self.textColor) {
attributes[NSForegroundColorAttributeName] = self.textColor;
}
if (self.font) {
attributes[NSFontAttributeName] = self.font;
}
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (void)slk_prepareForUndo:(NSString *)description
{
if (!self.undoManagerEnabled) {
return;
}
SLKTextView *prepareInvocation = [self.undoManager prepareWithInvocationTarget:self];
[prepareInvocation setText:self.text];
[self.undoManager setActionName:description];
}
@end