iOS Auto-Layout UILabel wrapping - ios

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

Related

UILabel and UIButton - truncate label instead of button

In my app I have a UITableView with a custom cell which contains a dynamic title label and a button next to it like in the following screenshot:
My problem is when the Title label gets too long, the button is truncated.
Instead i want the label to be truncated and the button should always be fully displayed. How can i achieve this?
(I do not want to set a fixed width for the button, since internationalization has to be supported too)
Set the priority of the constraint between label and button as 250 (low) . And set one more constraint which is trailing space from your title label to superview which should be little bit greater than button's width . Hope it helps you.
1) set a fixed width to button
Note: The button would be truncated in this case
2) Set Fixed Width to label
if you will set a fixed width to label, it would be same for iPad so it looks very ugly
you need to manage width based on UIScreen, If you set a fixed width to label
3) Using Content hugging priority and Content compression resistance priority
Set Following Constrain For label
set horizontal 250 in Content hugging priority and 749 in
Content compression resistance 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: UILabel not resizing properly with long text

I have a UITableViewCell with two UILabels: title and description.
I am having problems with the title label. Basically, what I want is to increase its height if the text is bigger than the available space and prevent the "...". This is how it looks, you can see the constraints of the title label on the right.
As you can see, the title label successfully increase its height but the text still in one line. The font used in "Text Styles - Callout". Any suggestions?
Edit 1: I already set number of lines to '0'
Edit 2: I fixed by deleting the UILabel and adding it again, but first take a look at suggestions made by Wojtek.
This behavior is supported with Auto-layout on, by following these steps for the UILabels in question.
Set number of lines to '0' in the Utilities pane > Attributes inspector.
Right click the UILabel and Alt - control (Xcode 7.1 and above) drag to it's superview to set the leading, trailing, top and bottom margin.
For auto height and width you can set the priority of the constraint to level '600' with relation 'Greater than or Equal'.
Github example (I only use a different 'relation' for the bottom constraint in the following example. You could also choose to do this for a different constraint i.e. the trailing constraint in your specific case): https://github.com/wojtekdmyszewicz/UILabel

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