How to implement a UIText field that increases in height as you type?

A very simple problem, but I did not find a solution. How to implement a UIText field that increases in height as you type? This will be the type of control that you see in iPhone sendmail for sending messages to iPhone. When you press a new line, the control increases in height.

+3
source share
2 answers

In your implementation, UITextViewDelegate textViewDidChange:you need to calculate the text size for a specific width and adjust accordingly.

-(void)textViewDidChange:(UITextView *)textView
{
    NSString *text = [textView text];
    NSFont *font = [textView font];
    CGFloat width = [textView frame].size.width;

    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:UILineBreakModeWordWrap];

    /* Adjust text view width with "size.height" */
}
+4
source

UITextView. TextDidChange ( , IB), .

+1

Source: https://habr.com/ru/post/1718829/


All Articles