diff --git a/MessagesTableViewController/JSBubbleView.h b/MessagesTableViewController/JSBubbleView.h index a360110..5a49822 100644 --- a/MessagesTableViewController/JSBubbleView.h +++ b/MessagesTableViewController/JSBubbleView.h @@ -53,5 +53,6 @@ typedef enum { + (CGSize)bubbleSizeForText:(NSString *)txt; + (CGFloat)cellHeightForText:(NSString *)txt; + (int)maxCharactersPerLine; ++ (int)numberOfLinesForMessage:(NSString *)txt; @end \ No newline at end of file diff --git a/MessagesTableViewController/JSBubbleView.m b/MessagesTableViewController/JSBubbleView.m index 7bbf02b..b4d4e6e 100644 --- a/MessagesTableViewController/JSBubbleView.m +++ b/MessagesTableViewController/JSBubbleView.m @@ -123,9 +123,8 @@ + (CGSize)textSizeForText:(NSString *)txt { CGFloat width = [UIScreen mainScreen].applicationFrame.size.width * 0.65f; - int numRows = (txt.length / [JSBubbleView maxCharactersPerLine]) + 1; - - CGFloat height = MAX(numRows, [txt numberOfLines]) * [JSMessageInputView textViewLineHeight]; + CGFloat height = MAX([JSBubbleView numberOfLinesForMessage:txt], + [txt numberOfLines]) * [JSMessageInputView textViewLineHeight]; return [txt sizeWithFont:[JSBubbleView font] constrainedToSize:CGSizeMake(width, height) @@ -149,4 +148,9 @@ return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 33 : 109; } ++ (int)numberOfLinesForMessage:(NSString *)txt +{ + return (txt.length / [JSBubbleView maxCharactersPerLine]) + 1; +} + @end \ No newline at end of file diff --git a/MessagesTableViewController/JSMessageInputView.h b/MessagesTableViewController/JSMessageInputView.h index 7f0830f..1acb4bd 100644 --- a/MessagesTableViewController/JSMessageInputView.h +++ b/MessagesTableViewController/JSMessageInputView.h @@ -41,6 +41,7 @@ @property (strong, nonatomic) UIButton *sendButton; #pragma mark - Message input view +- (void)adjustTextViewHeightBy:(CGFloat)changeInHeight; + (CGFloat)textViewLineHeight; + (CGFloat)maxLines; + (CGFloat)maxHeight; diff --git a/MessagesTableViewController/JSMessageInputView.m b/MessagesTableViewController/JSMessageInputView.m index de76d24..0a0b077 100644 --- a/MessagesTableViewController/JSMessageInputView.m +++ b/MessagesTableViewController/JSMessageInputView.m @@ -35,6 +35,7 @@ #import "JSMessageInputView.h" #import "JSBubbleView.h" +#import "NSString+JSMessagesView.h" @interface JSMessageInputView () @@ -61,7 +62,7 @@ - (void)setup { self.image = [[UIImage imageNamed:@"input-bar"] resizableImageWithCapInsets:UIEdgeInsetsMake(19.0f, 3.0f, 19.0f, 3.0f)]; - self.backgroundColor = [UIColor clearColor]; + self.backgroundColor = [UIColor whiteColor]; self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin); self.opaque = YES; self.userInteractionEnabled = YES; @@ -73,13 +74,13 @@ - (void)setupTextView { CGFloat width = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 246.0f : 690.0f; - CGFloat height = [JSMessageInputView textViewLineHeight] * [JSMessageInputView maxLines]; + CGFloat height = [JSMessageInputView textViewLineHeight]; self.textView = [[UITextView alloc] initWithFrame:CGRectMake(6.0f, 3.0f, width, height)]; self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; self.textView.backgroundColor = [UIColor whiteColor]; - self.textView.scrollIndicatorInsets = UIEdgeInsetsMake(13.0f, 0.0f, 14.0f, 7.0f); - self.textView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 13.0f, 0.0f); + self.textView.scrollIndicatorInsets = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 8.0f); + self.textView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f); self.textView.scrollEnabled = YES; self.textView.scrollsToTop = NO; self.textView.userInteractionEnabled = YES; @@ -97,6 +98,7 @@ self.frame.size.height)]; inputFieldBack.image = [[UIImage imageNamed:@"input-field"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.0f, 12.0f, 18.0f, 18.0f)]; inputFieldBack.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); + inputFieldBack.backgroundColor = [UIColor clearColor]; [self addSubview:inputFieldBack]; } @@ -133,9 +135,25 @@ } #pragma mark - Message input view +- (void)adjustTextViewHeightBy:(CGFloat)changeInHeight +{ + CGRect prevFrame = self.textView.frame; + int numLines = [JSBubbleView numberOfLinesForMessage:self.textView.text]; + + self.textView.frame = CGRectMake(prevFrame.origin.x, + prevFrame.origin.y, + prevFrame.size.width, + prevFrame.size.height + changeInHeight); + + self.textView.contentInset = UIEdgeInsetsMake((numLines >= 6 ? 4.0f : 0.0f), + 0.0f, + (numLines >= 6 ? 4.0f : 0.0f), + 0.0f); +} + + (CGFloat)textViewLineHeight { - return 35.0f; // for fontSize 15.0f + return 29.0f; // for fontSize 15.0f } + (CGFloat)maxLines diff --git a/MessagesTableViewController/JSMessagesViewController.m b/MessagesTableViewController/JSMessagesViewController.m index db96694..39132af 100644 --- a/MessagesTableViewController/JSMessagesViewController.m +++ b/MessagesTableViewController/JSMessagesViewController.m @@ -234,10 +234,14 @@ { CGFloat maxHeight = [JSMessageInputView maxHeight]; CGFloat textViewContentHeight = textView.contentSize.height; + BOOL isShrinking = textViewContentHeight < self.previousTextViewContentHeight; CGFloat changeInHeight = textViewContentHeight - self.previousTextViewContentHeight; - + changeInHeight = (textViewContentHeight + changeInHeight >= maxHeight) ? 0.0f : changeInHeight; - + + if(!isShrinking) // hackish -- to prevent ugly UI glitch with dynamic sizing of textview + [self.inputView adjustTextViewHeightBy:changeInHeight]; + if(changeInHeight != 0.0f) { [UIView animateWithDuration:0.25f animations:^{ @@ -254,6 +258,8 @@ inputViewFrame.size.height + changeInHeight); } completion:^(BOOL finished) { + if(isShrinking) // hackish -- to prevent ugly UI glitch with dynamic sizing of textview + [self.inputView adjustTextViewHeightBy:changeInHeight]; }]; self.previousTextViewContentHeight = MIN(textViewContentHeight, maxHeight);