Prerna chavan
Prerna chavan

Reputation: 3249

Adjust size and position of UITextField according to text entered

I have one UITextField in my application of width 150 pixels. When user add text, when text size exceeds beyond the width of textfield, textfield width should be increased accordingly so that the position is same and all text entered is displayed properly. I used following code but no luck.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    float width = [textField.text sizeWithFont:textField.font
                             constrainedToSize:CGSizeMake(300, textField.bounds.size.height)].width;

    if (width  > (textField.frame.size.width +50 )) {

        CGRect rect = textField.frame;
        rect.size.width +=10 ;

        textField.frame =rect;
    }

    return YES ;
}

Upvotes: 0

Views: 958

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

As you say, It will be of fixed length, then you can do in this way:

Set the max width of UITextField, and make the text centered align.

Upvotes: 2

Related Questions