Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto sizing UILabel when using Autolayout

Tags:

ios

uikit

I'm creating a xib using autolayout (NSLayoutConsraint). Is there a way to use sizeToFit or something similar to make a UILabel fit its contents?

I've tried setting a constraint of "greater than or equal to x" and tried setting the height to be below this. However, it keeps overwriting the height property. sitToFit doesn't seem to work when you have NSLayoutConstraints.

How can I get the label to size appropriately?

like image 883
James Avatar asked Oct 28 '12 01:10

James


People also ask

What is multiplier in Autolayout?

The multiplier which is represented as M in the equation is use to multiply (adjust) the relation by a certain of floating point value. We can say viewA. height should be *2 (two times) of viewB.

How is UILabel height calculated?

To summarize, you can calculate the height of a label by using its string and calling boundingRectWithSize . You must provide the font as an attribute, and include . usesLineFragmentOrigin for multi-line labels.

What is the use of Autolayout?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

What is UILabel?

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


1 Answers

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

like image 89
borrrden Avatar answered Oct 02 '22 06:10

borrrden