Reputation: 3249
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
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