Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change UILabel width not work with autolayout

Tags:

ios

autolayout

I have some code:

CGRect currentFrame = textLabel.frame;
CGSize max = CGSizeMake(textLabel.frame.size.width, 3000);
CGSize expected = [[textLabel text] sizeWithFont:textLabel.font constrainedToSize:max lineBreakMode:textLabel.lineBreakMode];
currentFrame.size.height = expected.height;
textLabel.frame = currentFrame;

expected.height = expected.height + 70;

[scrollView setContentSize:expected];

textLabel placed inside UIScrollView to display multiline text information.

In older version of application, without 4-inch screen support, everything was perfect. But now, unfortunately, resizing UILabel does not work.

Maybe, somebody can advice me, what should I change?

Thanks.

like image 210
Andrey Avatar asked Jan 21 '13 12:01

Andrey


1 Answers

When using AutoLayout you should not update the frame property, instead modify the contraints on a view.

On the other hand, you could also let AutoLayout work for you. Make sure the numberOfLines property of the label is set to 0 and the height constraint is of type Greater Than or Equal (>=). This way the layout will update automatically after setting new text on a label.

like image 150
Joris Kluivers Avatar answered Sep 20 '22 03:09

Joris Kluivers