Declan Land
Declan Land

Reputation: 650

UITextField - UIView Resize Programmatically

Am programmatically creating a UIView, setting the frame for the view, then adding a UITextField inside the view. I've been stuck on this one for a while, I'm trying to get the whole UIView to resize based on the amount of words in the text field if that makes sense?

Thanks, Declan

Upvotes: 1

Views: 483

Answers (1)

user3821934
user3821934

Reputation:

Assuming you have a given width, you can use sizeThatFits:

Here below is code I use for UITextViews. Similar code can be used for UITextFields.

Example:

UITextView *myTextView = [[UITextView alloc] init];
[myTextView setAttributedText:myAttributedString];
CGSize size = [myTextView sizeThatFits:CGSizeMake(width, FLT_MAX)];
CGRect frame = CGRectMake(0, 0, width, size.height);
myTextView.frame = frame;

Upvotes: 1

Related Questions