forked from facebookarchive/three20
/
TTTextEditor.m
350 lines (285 loc) · 9.83 KB
/
TTTextEditor.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
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
#import "Three20/TTTextEditor.h"
#import "Three20/TTDefaultStyleSheet.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
static CGFloat kPaddingX = 8;
static CGFloat kPaddingY = 11;
static CGFloat kOffsetY = 3;
static CGFloat kTextViewInset = 19;
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface TTTextEditorInternal : NSObject <UITextViewDelegate> {
TTTextEditor* _textEditor;
id<TTTextEditorDelegate> _delegate;
}
@property(nonatomic,assign) id<TTTextEditorDelegate> delegate;
- (id)initWithTextEditor:(TTTextEditor*)textEditor;
@end
@implementation TTTextEditorInternal
@synthesize delegate = _delegate;
- (id)initWithTextEditor:(TTTextEditor*)textEditor {
if (self = [super init]) {
_textEditor = textEditor;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if ([_delegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {
return [_delegate textViewShouldBeginEditing:textView];
} else {
return YES;
}
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
if ([_delegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {
return [_delegate textViewShouldEndEditing:textView];
} else {
return YES;
}
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
[_textEditor performSelector:@selector(didBeginEditing)];
if ([_delegate respondsToSelector:@selector(textViewDidBeginEditing:)]) {
[_delegate textViewDidBeginEditing:textView];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[_textEditor performSelector:@selector(didEndEditing)];
if ([_delegate respondsToSelector:@selector(textViewDidEndEditing:)]) {
[_delegate textViewDidEndEditing:textView];
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
if ([_delegate respondsToSelector:@selector(textEditorShouldReturn:)]) {
if (![_delegate performSelector:@selector(textEditorShouldReturn:) withObject:_textEditor]) {
return NO;
}
}
}
if (![_textEditor performSelector:@selector(shouldChangeText:inRange:) withObject:text
withObject:(id)range.location]) {
return NO;
} else {
SEL sel = @selector(textView:shouldChangeTextInRange:replacementText:);
if ([_delegate respondsToSelector:sel]) {
return [_delegate textView:textView shouldChangeTextInRange:range replacementText:text];
} else {
return YES;
}
}
}
- (void)textViewDidChange:(UITextView *)textView {
[_textEditor performSelector:@selector(didChangeText)];
if ([_delegate respondsToSelector:@selector(textViewDidChange:)]) {
[_delegate textViewDidChange:textView];
}
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
if ([_delegate respondsToSelector:@selector(textViewDidChangeSelection:)]) {
[_delegate textViewDidChangeSelection:textView];
}
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTTextEditor
@synthesize textDelegate = _textDelegate, textView = _textView, placeholder = _placeholder,
fixedText = _fixedText, minNumberOfLines = _minNumberOfLines,
maxNumberOfLines = _maxNumberOfLines, editing = _editing, autoresizesToText = _autoresizesToText,
showsExtraLine= _showsExtraLine;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_textDelegate = nil;
_internal = [[TTTextEditorInternal alloc] initWithTextEditor:self];
_placeholder = nil;
_fixedText = nil;
_autoresizesToText = YES;
_showsExtraLine = NO;
_minNumberOfLines = 0;
_maxNumberOfLines = 0;
_editing = NO;
_overflowed = NO;
_textView = nil;
_placeholderLabel = nil;
_fixedTextLabel = nil;
_textView = [[UITextView alloc] initWithFrame:CGRectZero];
_textView.delegate = _internal;
_textView.editable = YES;
_textView.opaque = NO;
_textView.backgroundColor = [UIColor clearColor];
_textView.scrollsToTop = NO;
[self addSubview:_textView];
}
return self;
}
- (void)dealloc {
[_internal release];
[_textView release];
[_placeholderLabel release];
[_placeholder release];
[_fixedText release];
[_fixedTextLabel release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updatePlaceholder {
if (_placeholder && !_editing && !_textView.text.length) {
if (!_placeholderLabel) {
_placeholderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_placeholderLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_placeholderLabel];
}
if (_textView.textColor == [UIColor whiteColor]) {
_placeholderLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1];
} else {
_placeholderLabel.textColor = TTSTYLEVAR(placeholderTextColor);
}
_placeholderLabel.font = _textView.font;
_placeholderLabel.textAlignment = _textView.textAlignment;
_placeholderLabel.text = _placeholder;
[self bringSubviewToFront:_placeholderLabel];
_placeholderLabel.hidden = NO;
} else {
_placeholderLabel.hidden = YES;
}
}
- (CGFloat)heightThatFits:(BOOL*)overflowed {
CGSize characterSize = [@"M" sizeWithFont:_textView.font];
CGFloat minHeight = _minNumberOfLines * characterSize.height;
CGFloat maxHeight = _maxNumberOfLines * characterSize.height;
CGFloat maxWidth = self.width - (kPaddingX*2 + kTextViewInset);
NSString* text = _textView.text;
if (!text.length) {
text = @"M";
}
CGSize textSize = [text sizeWithFont:_textView.font
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
CGFloat newHeight = textSize.height;
if ([text characterAtIndex:text.length-1] == 10) {
newHeight += characterSize.height;
}
if (_showsExtraLine) {
newHeight += characterSize.height;
}
if (overflowed) {
*overflowed = maxHeight && newHeight > maxHeight;
}
if (newHeight < minHeight) {
newHeight = minHeight;
}
if (maxHeight && newHeight > maxHeight) {
newHeight = maxHeight;
}
return newHeight + kPaddingY*2;
}
- (void)constrainToText {
CGFloat oldHeight = self.height;
CGFloat newHeight = [self heightThatFits:&_overflowed];
CGFloat diff = newHeight - oldHeight;
if (oldHeight && diff) {
if ([_textDelegate respondsToSelector:@selector(textEditor:shouldResizeBy:)]) {
if (![_textDelegate textEditor:self shouldResizeBy:diff]) {
return;
}
}
self.frame = TTRectContract(self.frame, 0, -diff);
}
}
- (BOOL)shouldChangeText:(NSString*)text inRange:(NSUInteger)location {
if (_fixedText && location < _fixedText.length) {
return NO;
}
return YES;
}
- (void)didChangeText {
if (_autoresizesToText) {
[self constrainToText];
}
}
- (void)didBeginEditing {
_editing = YES;
[self updatePlaceholder];
}
- (void)didEndEditing {
if (_editing) {
_editing = NO;
[self updatePlaceholder];
}
}
//- (void)_textViewDidChangeSelection:(UITextView *)_textView {
// // Workaround for weird bug - if user touches and holds placeholder, keyboard appears
// // but _textViewDidBeginEditing isn't called, so we call it here
// if (!_editing) {
// [self _textViewDidBeginEditing:_textView];
// }
//}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
- (void)layoutSubviews {
_textView.frame = CGRectMake(kPaddingX, kOffsetY, self.width-kPaddingX*2, self.height-kOffsetY);
if (!_overflowed) {
_textView.contentOffset = CGPointMake(0, 0);
}
_placeholderLabel.frame = CGRectMake(kPaddingX, 0, self.width-kPaddingX*2, self.height);
if (_fixedTextLabel) {
[_fixedTextLabel sizeToFit];
_fixedTextLabel.frame = CGRectMake(_textView.left+kPaddingX, _textView.top+kPaddingY,
_fixedTextLabel.width+2, _fixedTextLabel.height+4);
}
}
- (CGSize)sizeThatFits:(CGSize)size {
CGFloat height = [self heightThatFits:nil];
return CGSizeMake(size.width, height);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTextDelegate:(id<TTTextEditorDelegate>)delegate {
_textDelegate = delegate;
_internal.delegate = delegate;
}
- (NSString*)text {
return _textView.text;
}
- (void)setText:(NSString*)text {
if (_fixedText && text) {
_textView.text = [_fixedText stringByAppendingString:text];
} else {
_textView.text = text;
}
[self updatePlaceholder];
[self constrainToText];
}
- (void)setPlaceholder:(NSString*)placeholder {
[_placeholder release];
_placeholder = [placeholder copy];
[self updatePlaceholder];
}
- (void)setFixedText:(NSString*)text {
[_fixedText release];
_fixedText = [text copy];
if (_fixedText && !_fixedTextLabel) {
_fixedTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_fixedTextLabel.textColor = TTSTYLEVAR(placeholderTextColor);
_fixedTextLabel.font = _textView.font;
_fixedTextLabel.contentMode = UIViewContentModeBottom;
[self addSubview:_fixedTextLabel];
}
_fixedTextLabel.hidden = !_fixedText;
_fixedTextLabel.text = _fixedText;
}
- (void)scrollContainerToCursor:(UIScrollView*)scrollView {
if (_textView.hasText) {
if (scrollView.contentSize.height > scrollView.height) {
NSRange range = _textView.selectedRange;
if (range.location == _textView.text.length) {
[scrollView scrollRectToVisible:CGRectMake(0,scrollView.contentSize.height-1,1,1)
animated:NO];
}
} else {
[scrollView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
}
}
@end