Automatic UILabel Calibration Using Autolayout

I am creating xib using autolayout (NSLayoutConsraint). Is there a way to use sizeToFit or something similar so that the UILabel matches its contents?

I tried to set a limit of "greater than or equal to x" and tried to set the height below this. However, it retains the rewriting of the height property. The sitToFit function does not work if you have NSLayoutConstraints.

How can I appropriately label?

+7
source share
3 answers

Make an IBOutlet for width and height constraints and use sizeToFit to change their constant property and call needsUpdateConstraints

+4
source

Using "more than constraint" for UILabel height and calling sizeToFit inside viewDidLayoutSubviews helped

I did not need to call needsUpdateConstraints .

You also need to set the number of lines to 0 to allow an unlimited number of lines, or> 1 if you want a limit.

You must also set the preferredMaxLayoutWidth property, which is automatically set to the width of the views by the interface designer.

+1
source

You should try to lower the priority of the height limit.

0
source

All Articles