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

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

Related

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.

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.
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.

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.

Multiline UILabel and UITextView using Storyboards, Auto Layout and Swift

I am trying to add a Multiline UI Label as a header followed by a UITextView as the content body.
I have added the UILabel and UITextView as follows:-
The Label has number of lines set to 0, character wrapping and standard left, right, bottom and top constraints. I have also set a minimum height constraint but that does not seem to change anything. The UITextView below it has a top constraint to the label and left, right and bottom constraints to the view/layout guide.
The actual output is this:-
There should be 3 lines for this label but it is showing only 2 lines/
This label should have 2 lines and the text as "Tricyclic anti-depressants" but the last line is not visible.
Is there anything wrong with my setup/constraints?
Thanks
Do these two things:
Increase the number of lines of the label
Set the Height Constraint to greater than or equal
Make sure the top label's Vertical Compression Resistance priority is higher than the textview.

Allow UILabel to grow dynamically using auto layout

I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue.....
The even_venue.. label has 3 constraints for now:
Vertical space with eventt_title
a horizantal space with the leading of the superview
a width constraints which defines that the label width is always less than the superview.width.
What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?
This are possible steps which can create expandable UILabel
Set layouts for UILabel fixing its position
Set number of lines = 0
Set content vertical compression resistance to 1000 (this will allow text to push the label)
Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance
Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:
Vertical space with eventt_title
A horizontal space with the leading of the superview
A horizontal space with the trailing of the superview
Set UILabel's Line Breaks as Word Wrap.
Set UILabel's lines property as 0.
I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:
In the storyboard, place your label with the origin where you want it.
In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
In the code, set the text of the label using setText:
[self.myLabel setText:#"MyLabelText"];
This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.
I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.
I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.
AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.

Resources