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.
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.
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.
A view that displays one or more lines of informational text.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With