-
Notifications
You must be signed in to change notification settings - Fork 56
/
HPGrowingTextView.m
515 lines (400 loc) · 14.7 KB
/
HPGrowingTextView.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//
// HPTextView.m
//
// Created by Hans Pinckaers on 29-06-10.
//
// MIT License
//
// Copyright (c) 2011 Hans Pinckaers
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "HPGrowingTextView.h"
#import "HPTextViewInternal.h"
@interface HPGrowingTextView(private)
-(void)commonInitialiser;
-(void)resizeTextView:(NSInteger)newSizeH;
-(void)growDidStop;
@end
@implementation HPGrowingTextView
@synthesize internalTextView;
@synthesize delegate;
@synthesize font;
@synthesize textColor;
@synthesize textAlignment;
@synthesize selectedRange;
@synthesize editable;
@synthesize dataDetectorTypes;
@synthesize animateHeightChange;
@synthesize returnKeyType;
// having initwithcoder allows us to use HPGrowingTextView in a Nib. -- aob, 9/2011
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self commonInitialiser];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self commonInitialiser];
}
return self;
}
-(void)commonInitialiser
{
// Initialization code
CGRect r = self.frame;
r.origin.y = 0;
r.origin.x = 0;
internalTextView = [[HPTextViewInternal alloc] initWithFrame:r];
internalTextView.delegate = self;
internalTextView.scrollEnabled = NO;
internalTextView.font = [UIFont fontWithName:@"Helvetica" size:13];
internalTextView.contentInset = UIEdgeInsetsZero;
internalTextView.showsHorizontalScrollIndicator = NO;
internalTextView.text = @"-";
[self addSubview:internalTextView];
UIView *internal = (UIView*)[[internalTextView subviews] objectAtIndex:0];
minHeight = internal.frame.size.height;
minNumberOfLines = 1;
animateHeightChange = YES;
internalTextView.text = @"";
[self setMaxNumberOfLines:3];
}
-(void)sizeToFit
{
CGRect r = self.frame;
// check if the text is available in text view or not, if it is available, no need to set it to minimum lenth, it could vary as per the text length
// fix from Ankit Thakur
if ([self.text length] > 0) {
return;
} else {
r.size.height = minHeight;
self.frame = r;
}
}
-(void)setFrame:(CGRect)aframe
{
CGRect r = aframe;
r.origin.y = 0;
r.origin.x = contentInset.left;
r.size.width -= contentInset.left + contentInset.right;
internalTextView.frame = r;
[super setFrame:aframe];
}
-(void)setContentInset:(UIEdgeInsets)inset
{
contentInset = inset;
CGRect r = self.frame;
r.origin.y = inset.top - inset.bottom;
r.origin.x = inset.left;
r.size.width -= inset.left + inset.right;
internalTextView.frame = r;
[self setMaxNumberOfLines:maxNumberOfLines];
[self setMinNumberOfLines:minNumberOfLines];
}
-(UIEdgeInsets)contentInset
{
return contentInset;
}
-(void)setMaxNumberOfLines:(int)n
{
// Use internalTextView for height calculations, thanks to Gwynne <http://blog.darkrainfall.org/>
NSString *saveText = internalTextView.text, *newText = @"-";
internalTextView.delegate = nil;
internalTextView.hidden = YES;
for (int i = 1; i < n; ++i)
newText = [newText stringByAppendingString:@"\n|W|"];
internalTextView.text = newText;
maxHeight = internalTextView.contentSize.height;
internalTextView.text = saveText;
internalTextView.hidden = NO;
internalTextView.delegate = self;
[self sizeToFit];
maxNumberOfLines = n;
}
-(int)maxNumberOfLines
{
return maxNumberOfLines;
}
-(void)setMinNumberOfLines:(int)m
{
// Use internalTextView for height calculations, thanks to Gwynne <http://blog.darkrainfall.org/>
NSString *saveText = internalTextView.text, *newText = @"-";
internalTextView.delegate = nil;
internalTextView.hidden = YES;
for (int i = 1; i < m; ++i)
newText = [newText stringByAppendingString:@"\n|W|"];
internalTextView.text = newText;
minHeight = internalTextView.contentSize.height;
internalTextView.text = saveText;
internalTextView.hidden = NO;
internalTextView.delegate = self;
[self sizeToFit];
minNumberOfLines = m;
}
-(int)minNumberOfLines
{
return minNumberOfLines;
}
- (void)textViewDidChange:(UITextView *)textView
{
//size of content, so we can set the frame of self
NSInteger newSizeH = internalTextView.contentSize.height;
if(newSizeH < minHeight || !internalTextView.hasText) newSizeH = minHeight; //not smalles than minHeight
if (internalTextView.frame.size.height != newSizeH)
{
// [fixed] Pasting too much text into the view failed to fire the height change,
// thanks to Gwynne <http://blog.darkrainfall.org/>
if (newSizeH > maxHeight && internalTextView.frame.size.height <= maxHeight)
{
newSizeH = maxHeight;
}
if (newSizeH <= maxHeight)
{
if(animateHeightChange) {
if ([UIView resolveClassMethod:@selector(animateWithDuration:animations:)]) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
[UIView animateWithDuration:0.1f
delay:0
options:(UIViewAnimationOptionAllowUserInteraction|
UIViewAnimationOptionBeginFromCurrentState)
animations:^(void) {
[self resizeTextView:newSizeH];
}
completion:^(BOOL finished) {
if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
[delegate growingTextView:self didChangeHeight:newSizeH];
}
}];
#endif
} else {
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.1f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(growDidStop)];
[UIView setAnimationBeginsFromCurrentState:YES];
[self resizeTextView:newSizeH];
[UIView commitAnimations];
}
} else {
[self resizeTextView:newSizeH];
// [fixed] The growingTextView:didChangeHeight: delegate method was not called at all when not animating height changes.
// thanks to Gwynne <http://blog.darkrainfall.org/>
if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
[delegate growingTextView:self didChangeHeight:newSizeH];
}
}
}
// if our new height is greater than the maxHeight
// sets not set the height or move things
// around and enable scrolling
if (newSizeH >= maxHeight)
{
if(!internalTextView.scrollEnabled){
internalTextView.scrollEnabled = YES;
[internalTextView flashScrollIndicators];
}
} else {
internalTextView.scrollEnabled = NO;
}
}
if ([delegate respondsToSelector:@selector(growingTextViewDidChange:)]) {
[delegate growingTextViewDidChange:self];
}
}
-(void)resizeTextView:(NSInteger)newSizeH
{
if ([delegate respondsToSelector:@selector(growingTextView:willChangeHeight:)]) {
[delegate growingTextView:self willChangeHeight:newSizeH];
}
CGRect internalTextViewFrame = self.frame;
internalTextViewFrame.size.height = newSizeH; // + padding
self.frame = internalTextViewFrame;
internalTextViewFrame.origin.y = contentInset.top - contentInset.bottom;
internalTextViewFrame.origin.x = contentInset.left;
internalTextViewFrame.size.width = internalTextView.contentSize.width;
internalTextView.frame = internalTextViewFrame;
}
-(void)growDidStop
{
if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
[delegate growingTextView:self didChangeHeight:self.frame.size.height];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[internalTextView becomeFirstResponder];
}
- (BOOL)becomeFirstResponder
{
[super becomeFirstResponder];
return [self.internalTextView becomeFirstResponder];
}
-(BOOL)resignFirstResponder
{
[super resignFirstResponder];
return [internalTextView resignFirstResponder];
}
- (void)dealloc {
[internalTextView release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UITextView properties
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setText:(NSString *)newText
{
internalTextView.text = newText;
// include this line to analyze the height of the textview.
// fix from Ankit Thakur
[self performSelector:@selector(textViewDidChange:) withObject:internalTextView];
}
-(NSString*) text
{
return internalTextView.text;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setFont:(UIFont *)afont
{
internalTextView.font= afont;
[self setMaxNumberOfLines:maxNumberOfLines];
[self setMinNumberOfLines:minNumberOfLines];
}
-(UIFont *)font
{
return internalTextView.font;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setTextColor:(UIColor *)color
{
internalTextView.textColor = color;
}
-(UIColor*)textColor{
return internalTextView.textColor;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setTextAlignment:(UITextAlignment)aligment
{
internalTextView.textAlignment = aligment;
}
-(UITextAlignment)textAlignment
{
return internalTextView.textAlignment;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setSelectedRange:(NSRange)range
{
internalTextView.selectedRange = range;
}
-(NSRange)selectedRange
{
return internalTextView.selectedRange;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setEditable:(BOOL)beditable
{
internalTextView.editable = beditable;
}
-(BOOL)isEditable
{
return internalTextView.editable;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setReturnKeyType:(UIReturnKeyType)keyType
{
internalTextView.returnKeyType = keyType;
}
-(UIReturnKeyType)returnKeyType
{
return internalTextView.returnKeyType;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setDataDetectorTypes:(UIDataDetectorTypes)datadetector
{
internalTextView.dataDetectorTypes = datadetector;
}
-(UIDataDetectorTypes)dataDetectorTypes
{
return internalTextView.dataDetectorTypes;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)hasText{
return [internalTextView hasText];
}
- (void)scrollRangeToVisible:(NSRange)range
{
[internalTextView scrollRangeToVisible:range];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UITextViewDelegate
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if ([delegate respondsToSelector:@selector(growingTextViewShouldBeginEditing:)]) {
return [delegate growingTextViewShouldBeginEditing:self];
} else {
return YES;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
if ([delegate respondsToSelector:@selector(growingTextViewShouldEndEditing:)]) {
return [delegate growingTextViewShouldEndEditing:self];
} else {
return YES;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([delegate respondsToSelector:@selector(growingTextViewDidBeginEditing:)]) {
[delegate growingTextViewDidBeginEditing:self];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)textViewDidEndEditing:(UITextView *)textView {
if ([delegate respondsToSelector:@selector(growingTextViewDidEndEditing:)]) {
[delegate growingTextViewDidEndEditing:self];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)atext {
//weird 1 pixel bug when clicking backspace when textView is empty
if(![textView hasText] && [atext isEqualToString:@""]) return NO;
if ([atext isEqualToString:@"\n"]) {
if ([delegate respondsToSelector:@selector(growingTextViewShouldReturn:)]) {
if (![delegate performSelector:@selector(growingTextViewShouldReturn:) withObject:self]) {
return YES;
} else {
[textView resignFirstResponder];
return NO;
}
}
}
return YES;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)textViewDidChangeSelection:(UITextView *)textView {
if ([delegate respondsToSelector:@selector(growingTextViewDidChangeSelection:)]) {
[delegate growingTextViewDidChangeSelection:self];
}
}
@end