Adjust height of UILabel dynamically using iOS storyboard - ios

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.

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.

Related

Equal height constraints issue iOS Interface builder

I have four UILabel in a Row. Label's title is changing dynamically based on some conditions. Title may be multiline or single line. So all the labels should maintain same height means height of the Max height label. I added Equal height constraints for all labels using storyboard, But issue is suppose one Label have multi line it simply neglects the height and shows only one line, if two labels have multi line all the labels maintain the multi line height and shows correctly. I tried different constraints combinations but result is same, Maintain height only if two of them have bigger heights.
remove Height and Equal Height Constrain.
Just add Constrain from Top,Bottom,Left Right except Last label.
in last label Add Left,Right and Height Constrain
and use number of line will zero to all Label :)
Got a new idea. After u set equal heights change the compress / hugging priority ( higher value means harder to change )

can not set layout correctly when adding several uilabels to uitabelviewcell

I came across a problem when configuing the cell of UITableView. I add two labels vertically in the content view of the UITableViewCell, and I also add constraints for the top, leading and bottom layout attributes:
I think that the height of the cell can be caculated dynamically as I have set all the vertical layout and with the instrinct size of the label, the height can be inferred.
so, I can not understand the error message that IB told me.
The second problem is that the height of cell appear on the IB is not changed with the constraint I`ve make. If I decrease the bottom constraint for example, and it is the label to change its size to fit the constraint, but not the cell change its height.
If you need to add top , leading and trailing(or width) to the 1st label. Then add bottom ,leading and trailing(or width)for the bottom label. Then add bottom constraint for 1st label to 2nd label.Then by selecting both labels, add equal height constraint.It will solve your problem.
The meaning of this conflict is that when your label content is increasing dynamically, which label's content is needed to give more priority before whom.
More precisely it can be said that if you increase one of the label's content hugging priority i.e. 252 then that label's content increment and size will be given more priority for incrementing it foremost. As autolayout executes according to the priority of constraints, it faces ambiguity in terms of increasing the views of labels if you do not set the content hugging priority.

UILabel keeps resizing to a height of 0 regardless of the text

I have a UILabel with the following constraints
and I set the number of Lines equal to 0. The layout on IB can be seen below. I want this UILabel to expand dynamically based on the text that I receive where the views below it get pushed down based on the height of the UILabel. However, that is not what happens.
As seen below, the UILabel doesn't appear at all. It seems to have a height of 0 regardless of what I set the text to be. Does anyone know what I need to modify to make this happen? Does anyone know why the UILabel has a height of 0?
UPDATES:
Things I tried given the comments below.
word wrap, nothing changed
adding a height constraint on UILabel, text gets cut off after the first line
height constraint with greater than or equal to constant, text still gets cut off after the first line
I created a dummy View according to your requirement.
Button has fixed constraints : leading width ,height, top space.
For label : leading, trailing, topSpace to button
3.For View below the label: give leading, trailing, height , top space to label
so here label height is not fixed .so it will change according to text.
After setting the text in label try out "labelName.sizeToFit()" this will automatically adjust the height of your label.
You should try to set a height constraint to your Label.
Considering the information you provided, it seems like it doesn't have enough height because you are using bottom constraint to the view under it. Rearrange your constraints or give a height constraint to the UILabel.
Select the UILabel,Set the Line Breaks mode is Word Wrap and set the number of lines is 0, example is given below,
And set the Height Constraints, If you set height contraints then select the UILabel, see the right side, select show the size Inspector --> Double click the height Contraints --> Relation --> set the Greater Than or Equal, its automatically expand the label, example is given below,
hope its helpful

Autolayout Height >= 0 Frame in Interface Builder

I'm using interface builder to set up a table view cell with two labels. One of them should have dynamic height, depending on how much text it holds. (The cell is also of dynamic height based on the height of that label.) I have the constraints working properly – both the label and the cell resize correctly based on input text. My problem is that to make it work I had to set a constraint on the resizing label of height >= 0. For some reason, IB forces me to make the on-screen height of the label 0, which makes it hard to see/work with. The little dots below "Label" is the 0-height label:
If I set it at a height greater than 0 (which should be allowed by the constraint) the constraint turns red in error:
I'm curious why IB won't let me set something with a >= constraint on its height at a height greater than the minimum value. Is there a way I can get rid of this error?
While working with label text you don't require to give the height to UILabel. As it will automatically take the height based on font size. All you need to do is give vertical,top and bottom constraint to both the labels and remove the default text from attributes inspector. Now you can create table cell with dynamic height.
Top Label constraints:
leading,Trailing,Top
Bottom Label constraints:
leading,Trailing,Bottom
and also give vertical spacing between top and bottom label.
And with the self sizing cell height you can get you desired result.
ref url: self-sizing-cells.
Here is the image of constraints which i've set:
Here is the image of constraints for both the label.
And the sample code which i've added to display data with result.
Hope it helps in solving your problem.

XCode Interface Builder Auto Layout issue

i have UILabel (red colored) on top and UIScrollView (blue colored) at the bottom of it. I have set all constraints for UILabel and at the same time I have set UIScrollView Top Space To: UILabel = 10.
Now when my app is loaded everything looks good. But, i am dynamically changing height of my UILabel depending on its text and logically when height of UILabel increases UIScrollView should automatically go down to keep constraint. But this does not happen. ScrollView stays as it is and overlaps with increased UILabel.
Here is what you should do.
With all of your current settings,
Don't use height constraint for label,
set number of line to 0 to label
You don't need to add any calculation of height of label, it will adjust it's height according to text
PS: if you want to have some height constraint to set minimum height of label, you can use "Greater or equal to instead of equal"

Resources