Question

I'm using this code to make a dynamic UITextView with auto resize

- (void)textViewDidChange:(UITextView *)txtView
{
    CGRect frame;
    frame = txtView.frame;
    frame.size.height = [textView contentSize].height;
    textView.frame = frame;
}

the code is fine, but when i press "return" in the keyboard (break line), I need the textView to grow UP direction and not down like it is now.

I tried to do something with the "CGRectGetMaxY(textView.frame)" becuase I know that the bottom of the textView need to stay as it is and not change, but I didn't find the right way.

This textView using for a chat message output, so if the user writing something long, the textView need to grow up becuase the keyboard is bellow

Was it helpful?

Solution

This should do the trick:

- (void)textViewDidChange:(UITextView *)textView
{
    CGRect frame = textView.frame;
    frame.origin.y = CGRectGetMaxY(frame) - [textView contentSize].height;
    frame.size.height = [textView contentSize].height;
    textView.frame = frame;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top