@@ -396,41 +396,18 @@ - (void)textViewDidEndEditing:(UITextView *)textView
396
396
[textView resignFirstResponder ];
397
397
}
398
398
399
- - (void )textViewDidChange : (UITextView *)textView
400
- {
399
+ - (void )refreshViewFrames : (UITextView *)textView {
401
400
CGFloat maxHeight = [JSMessageInputView maxHeight ];
402
401
403
- // TODO:
404
- //
405
- // CGFloat textViewContentHeight = textView.contentSize.height;
406
- //
407
- // The line above is broken as of iOS 7.0
408
- //
409
- // There seems to be a bug in Apple's code for textView.contentSize
410
- // The following code was implemented as a workaround for calculating the appropriate textViewContentHeight
411
- //
412
- // https://devforums.apple.com/thread/192052
413
- // https://github.com/jessesquires/MessagesTableViewController/issues/50
414
- // https://github.com/jessesquires/MessagesTableViewController/issues/47
415
- //
416
- // BEGIN HACK
417
- //
418
- CGSize size = [textView sizeThatFits: CGSizeMake (textView.frame.size.width, maxHeight)];
419
- CGFloat textViewContentHeight = size.height ;
420
- //
421
- // END HACK
422
- //
423
-
424
- BOOL isShrinking = textViewContentHeight < self.previousTextViewContentHeight ;
425
- CGFloat changeInHeight = textViewContentHeight - self.previousTextViewContentHeight ;
426
-
402
+ BOOL isShrinking = textView.contentSize .height < self.previousTextViewContentHeight ;
403
+ CGFloat changeInHeight = textView.contentSize .height - self.previousTextViewContentHeight ;
404
+
427
405
if (!isShrinking && (self.previousTextViewContentHeight == maxHeight || textView.text .length == 0 )) {
428
406
changeInHeight = 0 ;
429
407
}
430
408
else {
431
409
changeInHeight = MIN (changeInHeight, maxHeight - self.previousTextViewContentHeight );
432
410
}
433
-
434
411
if (changeInHeight != 0 .0f ) {
435
412
436
413
[UIView animateWithDuration: 0 .25f
@@ -463,12 +440,33 @@ - (void)textViewDidChange:(UITextView *)textView
463
440
completion: ^(BOOL finished) {
464
441
}];
465
442
466
- self.previousTextViewContentHeight = MIN (textViewContentHeight, maxHeight);
443
+ self.previousTextViewContentHeight = MIN (textView.contentSize .height , maxHeight);
444
+ }
445
+
446
+ // Once we reached the max height, we have to consider the bottom offset for the text view.
447
+ // To make visible the last line, again we have to set the content offset.
448
+ if (self.previousTextViewContentHeight == maxHeight && [textView.text hasSuffix: @" \n " ]) {
449
+ double delayInSeconds = 0.01 ;
450
+ dispatch_time_t popTime = dispatch_time (DISPATCH_TIME_NOW, (int64_t )(delayInSeconds * NSEC_PER_SEC));
451
+ dispatch_after (popTime, dispatch_get_main_queue (), ^(void ){
452
+ CGPoint bottomOffset = CGPointMake (0 .0f , textView.contentSize .height - textView.bounds .size .height );
453
+ [textView setContentOffset: bottomOffset animated: YES ];
454
+ });
467
455
}
468
456
469
457
self.messageInputView .sendButton .enabled = ([textView.text js_stringByTrimingWhitespace ].length > 0 );
470
458
}
471
459
460
+ #pragma mark - KVO observer method
461
+
462
+ - (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context {
463
+ UITextView *textView = (UITextView *)object;
464
+ if ([textView isKindOfClass: [UITextView class ]] && [keyPath isEqualToString: @" contentSize" ]) {
465
+ [self refreshViewFrames: textView];
466
+ }
467
+ }
468
+
469
+
472
470
#pragma mark - Keyboard notifications
473
471
474
472
- (void )handleWillShowKeyboardNotification : (NSNotification *)notification
0 commit comments