Autolayout: UILabel not resizing properly with long text - ios

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

Related

iOS Swift Multiline UILabel with StackView Inside UITableViewCell

I have three UILabels inside a UIStackView which is inside a UITableViewCell. All my labels have lines set to 0. Problem is labels sometimes gets disappeared or shows partial texts.
Things I have tried:
Embedded each label inside UIView and set Constraints to them so take full UIView size. In this case, StackView had both alignment and distribution set to fill.
StackView set alignment set to leading and UIVIews had same setup as the first try.
Tried without the StackView but still, Labels doesn't show full text or gets disappeared.
A sample Stackview setup I tried
Output I am getting:
As seen on the image, Arabic texts get disappeared (Shown with red notation). Some English texts are show partially. (Shown in yellow)
Other answers on StackOverflow I followed:
Multiline label in UIStackView
Multiline label in UIStackView
UIStackView and multi-line labels in a UITableViewCell
Multi Line Labels and an Image with UIStackView
Try 1: From an answer:
According to the suggestion of Talha, I made the following changes
But no luck. The output I got from the changes:
Set the label's constraint to (top, left, right, bottom , equal hight). Then you have to change equal height constraint value "greater than equal" from the attribute inspector. Now change the label attributes "line break=word wrap" , "lines=0"

Why doesnt my textview show up when I add autolayout constraints?

I have a view which contains a textview (which displays a question) at the top. And 4 labels (for various answer options) with a stack of option buttons at the foot.
When leave the 'question' textview without constraints it appears fine in portrait. When I rotate the handset the text doesnt expand across the width of the screen.
I thought the way to resolve this was to click the add constraints pin/align button and click the T bars adding '0' constraints for top trailing and leading.
However when I do that the textview doesn't display at all. Any idea why?
The height constraint is missing. When you don't provide any constraints to your UITextView Xcode automatically will add the constraint's but as soon as you try to pin the top, leading and trailing anchors Xcode will not add any constraint's automatically.
You have to pin the bottom anchor of the UITextView to any of your buttons or give it a height constraint will solve your problem. Because UITextView will not calculate it's height based on it's inner content size like UILabel or UITextField.
If you want to only display some text and no edit functionality then you can use a UILabel and set the number of lines to 0.

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

Xcode IB auto sizing?

Everything looks fine in IB. When I run my app using an iPhone5/iPhone6/iPad sims, it's completely mangled.
In IB:
In iPhone6 sim:
In the above sim, the buttons are cut. The textfield and textview are also cut. For the TF & TV, they are aligned center but trail off the edges on both sides.
I have the buttons width set
Editor > Pin > Widths Equally
The title label is set with a
Horizontal Center in Container
constraint
The textfield and textview also have
Horizontal Center in Container
and
Editor > Pin > Width
Is there some way to fix this?
-- EDIT --
After a few tries with constraints, looks like I have everything working except the two buttons.
Current listing of constraints:
As suggested by others, you should consider using the Auto-layout feature if you plan on constructing your views using the IB.
Here are some tutorial links:
RayWenderlich.com Part One
Youtube video This one covers a bit of size clases
Hope they help.
UPDATED:
I've read your updated post, you need to add width and height constraints. The view you see in the IB right now is 600 by 600 points, and the simulator one is smaller, which means that if you leave it as is, when you run the app you'll only see what the iphone screen has the capacity to show.
You need to add more constraints than what you used, try defining an equal width for the buttons, and assign the left one a left margin constraint, the right one a right margin constraint, give both of them vertical spacing constraint related to the text view or long label, the text view or long label should have left and right margin constraints, a height constraint and a top constraint to the textfield, the textfield should have a vertical spacing constraint to the label, also left and right and height constraint, and last the label should have leff, right and height constraints plus a top constraint to the main view.
I feel that those are all you need, but Xcode will through warnings at you if it feels you are missing something.
Try it, and let us know.

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