dynamically resize textview. closes #8.

This commit is contained in:
Jesse Squires
2013-03-18 01:39:23 -04:00
parent 605bc29b17
commit a96c829b4e
5 changed files with 40 additions and 10 deletions

View File

@@ -53,5 +53,6 @@ typedef enum {
+ (CGSize)bubbleSizeForText:(NSString *)txt;
+ (CGFloat)cellHeightForText:(NSString *)txt;
+ (int)maxCharactersPerLine;
+ (int)numberOfLinesForMessage:(NSString *)txt;
@end

View File

@@ -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

View File

@@ -41,6 +41,7 @@
@property (strong, nonatomic) UIButton *sendButton;
#pragma mark - Message input view
- (void)adjustTextViewHeightBy:(CGFloat)changeInHeight;
+ (CGFloat)textViewLineHeight;
+ (CGFloat)maxLines;
+ (CGFloat)maxHeight;

View File

@@ -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

View File

@@ -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);