Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust height of UILabel dynamically using iOS storyboard

I have a label, which has been created using iOS Storyboard layout.

Contents of label, however, are dynamic and can change during run time.

How do I make sure that label's height gets adjusted based on the content in the label.

I tried:

Setting number of lines to 0

Setting Editor -> Size to Fit Content.

But they don't work. Text in label gets printed only in single line, and as a result of that some of text doesn't appear on the screen.

Any help would be highly appreciated.

like image 263
user462455 Avatar asked Jul 20 '16 02:07

user462455


People also ask

How do I change the dynamic height of a label in Swift?

To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height.

How do I create a dynamic label in Swift?

Dynamic label could be achieved by setting correct constraint, numberOfLines = 0 and lineBreakMode . Output looks like this: Issue is we have to restrict text within maximum height.

What is UILabel in Swift?

A view that displays one or more lines of informational text.


2 Answers

Image Try this way

Your lable should be number of lines to 0

give height constraint to lable and select height constraint then set greater than equal , it will automatically adjust height based on content

like image 164
karthikeyan Avatar answered Nov 15 '22 18:11

karthikeyan


Please check your constraints. You can't set label's height constraint.If superview's height is fixed,you can't set top and bottom constraint at same time.Because it means you set the height constraint of label. So you can must fix label width and you can't fix label height.

like image 37
kai Avatar answered Nov 15 '22 18:11

kai