Multiline UILabel and UITextView using Storyboards, Auto Layout and Swift - ios

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.

Related

Make label height as content in it without spacing?

I have added a label on storyboard. I have given it line as 0. It's height is not static. But when there more lines then it height increased but space is added from top & bottom . I want to remove those extra space when label height is increased. I have tried using label.sizeToFit() but it align text to the left & I want text to be in centre. Only extra spacing from top & bottom should be reduced.
Here I tried following steps with SafeAreaLayout/AutoLayout to solve this problem.
For label instance in storyboard (view controller) or programmatically
Remove all (existing) constraints applied on your label.
Set all constraints (top, bottom, right left) again, with respect to its superview/adjacent UIElements.
Set number of lines = 0 (& line break mode = truncate tail)
Here is result:
Edit: Answer to your query -- my label is vertical centre to a super view
Remove Top constraint for your vertically centered label and apply constraint: Vertically in container = 0
Result with update (Vertically Center):
Edit : Make sure to decrease font size as when font becomes large , intrinsic add more space around it
contentHuggingPrioprity vertical set it to 1000
In addition to sizeToFit you could set
label.textAlignment = .center
You could also add an UIView with constaints set like the label is set right now. Inside that View you add the label with constraints to top, left and right and the height to be greater than or equal to 0.

iOS Auto-Layout UILabel wrapping

I'm trying to get my subtitle label to wrap, but it always remains one line with truncated text. I have it so the subtitle has a top constraint of 5px to the title/SignIn label, and 20px to the separator.
I've been working with Content Hugging and Compression Resistance but I haven't been able to figure it out.
If I remove the bottom constraint, the label becomes 3 lines as desired, but overlaps the separator (i.e. the content doesn't get pushed down).
Any help would be greatly appreciated.
Set lines to 0. Then enlarge the label to fit in the required lines of code i.e, increase the height of the label on storyboard. Then set the height constraint of the label from the pin menu to that height.
So, auto updating my project's settings and restarting Xcode/Simulator had something to do with it!
Everything seems to be working now (with the following settings for the subtitle):
Content hugging has been set to: h=251 & v=251
Content compression resistance has been set to: h=750 & v=750
Number of Lines has been set to: 0
Line Break Mode has been set to: Truncate Tail
"Bottom space to separator" constraint has been set to: Equals 20
"Top space to sign in label" constraint has been set to: Equals 5

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

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