erotsppa
erotsppa

Reputation: 15021

How do I implement a UITextfield that increases in height as I type?

A very simple problem but I have found no solution. How do I implement a UITextfield that increases in height as I type? This would be the type of control you see in iPhone's native SMS app send bar. When you press new line, the control increases in height.

Upvotes: 3

Views: 3905

Answers (2)

Jeremy
Jeremy

Reputation: 849

I would use a UITextView instead. In either case you can use the TextDidChange notification (called editing changed in IB) to check the size of your text and update the height accordingly.

Upvotes: 1

Nick Bedford
Nick Bedford

Reputation: 4435

In your UITextViewDelegate implementation of textViewDidChange: you need to calculate the size of the text for the 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" */
}

Upvotes: 4

Related Questions