Two stacked dynamic height labels in UITableViewCell - ios

I am trying to have two labels stacked in a UITableViewCell. I have AutomaticDimension and estimatedRowHeight set on my tableView.
I want the cells to show up stacked, with their dynamic heights if they both have content.
If only one has content, I would like it to be centered in the cell, or rather have the cell collapse to only show that label.
I have tried every constraint combo I can think of, but usually the labels either do not expand, or I end up with filler space where the label should be.
If the label has no text, I am setting label.text = "".
I am calling sizeToFit() on the labels each time I set the text.
What is the correct constraint combo? Here is what I currently have to start with.

you have to make two labels with leading, trailing, top and bottom constrains and don't add height constrains
make the number of lines for labels = 0
check the title label and from size inspector menu click on the vertical content hugging priority to be 250 not 251 as shown in the screen
image
in the viewController connect your tableview and add these lines
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44

This could be done using StackViews in iOS,
in the Storyboard Select your both Labels and embed them inside a UIStackView
one of the benefits if you declared a good constraints it should automatically handle the requested behavior.
I recommend this tutorial to get started with UIStackView here

Better solution is UIStackView.
If still you want to go with UILabel you can try one more thing:
Setting label.text = "" will decrease the height of label but the constraint for distance between the labels will not make the label in centre.
For that add a referencing outlet of distance constraint and set it :
distanceConstraint.constant = 0
programatically while setting the label.text = "".
This will set the label with data in centre.
Hope this helps.

Related

How to give dynamic height to labels contained withing stackview in swift

Inside a UItableview Cell I have following structure.
It has two Stackviews Left and Right
1) Left Stackview contains 3 views. Each view containing 1 label (Label1,Label2,Label3)
2) Right Stackview contains 2 views. Each view containing 1 label (Label5,Label6)
Constraints for Label1,Label2 and Label3 are Top,Left,Right and Bottom with respect to it's parent views.
Now when I add text to Label1 , Label2 and Label3 at run time it gives dynamic height but leaves space in top and bottom, for those label having less text. Below is the output.
I want to reduce the top and bottom spaces for all the three labels i.e Label1,Label2,Label3.
Any help will be appreciated.
This may help you
I think You have use distribution of Stack view as fill equally so it gives equal space to each one.
I think for this you have to use fill proportionally. this will adjust height accordingly
Choose fill proportionally from here
if this not working then choose fill proportionally and give height to each label it will adjust.
Check how to give height to labels .
after giving each label height inside stack view like this .
your it will adjust itself according to data.Like this
For dynamic height use self sizing cells in tableview. Set rowHeight and estimatedHeight properties for tableView
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
Set the stackview distribution property to .fillproportionally
as per the UIStackView documentation of .fillproportionally property
A layout where the stack view resizes its arranged views so that they
fill the available space along the stack view’s axis. Views are
resized proportionally based on their intrinsic content size along the
stack view’s axis.
in code:
stackView.distribution = fillproportionally
or in storyboard
Please check the attached screenshot, there are showing fully step by step guide line. How to set UILabel dynamically height inside vertical stackview with multiple UILabel's.
If you have any queries please comment.
Check the attached screenshot

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 - Swift - iOS - Expanding TableView Cells Based on Title & Subtitle Sizes

I have a TableView that uses custom cells. These custom cells have three labels in them. The UIViewController looks like this:
I am trying to make it so that depending on the length of the title, location, and time labels, the cell will expand accordingly. I have set the number of lines each of the labels can take up to 0 (infinite). The TableView itself is constrained to the sides and bottom of the ViewController, and on the top to the thin blue UIView. The constraints of the three labels within the cell are as follows:
"Title" label constraints:
"Location" label constraints:
"Time" label constraints:
The problem is that when I run the simulator, I see that the labels, when the are too long for the cell, simply have "..." on the end, and don't go to the next line. They always stay one line. I want the text of the labels to go to next lines as needed and for the cell to expand accordingly.
In the code I have the lines:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0
And I have the appropriate code the set the cells for the tableView, making sure to use the custom cell I have set up.
Make sure to set the appropriate Content Compression Resistance Priority. This means that the label should try to resist "being squished" or compressed. Try making them slightly different for each label in case the constraints "compete" for some reason. For example, make the TitleLabel's vertical priority 752, LocationLabel 751, and TimeLabel 750.
I ran into the same problem. After battling for ages I found that reducing the horizontal compression resistance settings for the labels to 250 (top label) and 251 (bottom label) worked!
I don't fully understand why, and have posted a question here to try find answers: https://stackoverflow.com/questions/33236691/how-does-reducing-horizontal-compression-resistance-for-a-label-affect-row-heigh

Setting constraints for dynamic cell

I'm new to using constraints in my iOS projects, and trouble setting the right constraints for my dynamic UITableViewCell. I've tried every combination I can think of but it either won't dynamically change height, or it gives me warnings about ambiguous layouts.
My first label1 is not supposed to change in height, but the other two are. My current constraints achieve the desired effect, but are giving me the warnings seen under.
The warnings go away if I constrain the height of the labels (obviously), but that doesn't solve my problem.
Any suggestions on how to solve this would be appreciated!
First of all you need to set
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
in viewDidLoad of your UITableViewController
Then you need to set constraints from top to bottom of the cell
top space to superview
vertical spacing
bottom space to superview
height greater or equal
and of course set lines to 0 for every UILabel
Example project is available here https://github.com/MihaelIsaev/SwiftAutoResizableCells

Resources