Two uilabels on uitable with dynamic height - ios

I am trying to implement one English to Swedish learning app for Swift/IOS there I want to have two dynamic rows, data is coming from array and I tried my best to fix it out. I have seen approx. all stack overflow links as well but no success at all.
See the screenshot, as the text is long, I cannot see it on the screen and the text is not coming on the second line as well. As, I add any constraint, both lines sometimes disappeared or only one lines comes.
I have seen this tutorial link as well but cannot get any success.
Can any please see the code and fix it.
Here is the complete code

You have to use UITableViewAutomaticDimension to manage the cell height as per the content. For that you have to set the constraint of your labels from all the sides, ie Click on the PIN and then uncheck the 'Constraint from margin' check box and for both of your labels add constraints from Top, bottom, Left and Right side.As it is required if you want to use UITableViewAutomaticDimension.
Now in the ViewController in viewDidLoad method add this two line
self.tableView.estimatedRowHeight = 44.0
self.tableView.rowHeight = UITableViewAutomaticDimension
and then you your labels will display full text.

Related

minimum number of lines UILabel in iOS development

I was trying to make a cell in uicollectionview. please see these two images
Thats the cell of a collectionview and the first uilabel's number of line is 2. what I am trying to do is the uilabel will always take 2 lines of height regardless of the content of the uilabel, is it possible? if the text is short, second line will be blank.
If I add height constraint, the text is vertically centred. How to make it top-aligned?
There are 2 steps.
First select your label, click the add new constraints, and check the height constraints, as grow4gaurav said.
Next, go to the attributes inspector and set the number of lines to 0. This makes it so that the text uses as many lines as it wants. So, if the text is short, and it only uses one line, it will just use the top line. If it is longer, it will use the bottom line too.
Hope this helps

Swift dynamic table text goes outside cell width

i just created a uitable view with dynamic cells, and as you can see with the insets, the cells have a correct width. But as shown on the image, the label goes out of the screen. I've set truncate tail, fixed font size... I can't manage to get this text truncated.
Should i do it programatically when creating the cell.text.value?
Help is very much appreciated here.
You might not have set frame in LayoutSubviews(). Initially when tableviewcell loads it always gives same width value but when layoutSubviews gets called it gives correct frame.
If you have done it in storyboard then you might have given hardcoded value.
Use Custom TableViewCell's
You can design the way you want and also you can set label/title width height font etc easily .
Make sure you register your tableview cell in viewdidload() or else it will crash
Here are two stack views, each with the same content - the text of the top label is this is a really long label that will extend past the right edge
The top one does not have a Trailing constraint.
The bottom one does have a Trailing constraint of 8.
That really looks like the issue you are running into.
Solved the issue. Initially i had dropped a table view inside the view controller and set the prototype cell to 1 in IBinspector. For an unknown reason, the constraints were not working. Ended up setting protoype cells to 0 and dropping a new table view cell from the object library.
That did the trick.

Horizontal UILabels inside a self sizing UITableViewCell

Question: How can I use constraints in a UITableViewCell using UITableViewAutomaticDimension when there are labels side by side which can each contain a different number of lines?
I have a table with cells that contain labels side by side:
I'm having a hard time getting the cell to size properly if one of the labels on one side have more lines than the corresponding label on the other side. One of the labels always gets cut off. If both labels have the same number of lines, the cell resizes perfectly and all text can be seen.
I've tried a number of constraint tweaks but nothing is working perfectly as desired.
I've tried adding hidden label for height...but it doesn't work well for the cell with 4 labels
I've tried placing the labels in a view but then the height of the view still needs to be calculated.
Currently the constraints are setup normally: top, bottom, left , right, labels are set to 0 lines etc and works great when both labels have the same text or same number of lines.
Is there any way I can do this with constraints? Or do I need to revert to the old height for row at index path? =(
There is a way to achieve what you want to do just using constraints. The trick is to set the relation of the constraints between the two lower labels and the bottom to Greater Than or Equal instead of equal.
Here is how I set the constraints:
Which has this result:
Or you could set the constraints between the labels top and bottom to Greater Than or Equal and leave the bottom constraint at equal:
To get this result:
I found a solution where I did not have to revert to using tableView:heightForRowAtIndexPath.
I added a label to the cell with the text color set to clearColor and called it 'heightLabel'. I added constraints to the heightLabel so the cell would resize based on the heightLabel's constraints.
Then whichever of the left or right labels had the most text, I placed that label's text inside the heightLabel.
The cells still were not resizing and I had to call 'layoutIfNeeded' on the cell before returning the cell in tableView:cellForRowAtIndexPath.
The heightLabel constraints are now what is being used to resize the cell and UITableViewAutomaticDimensions is working properly.
This may not be the best solution but it's working out pretty well. If there is a more proper way of doing this I'm open to suggestions.

How to keep auto-sized table cells format consistent after edition/deletion of rows using UIKit?

I have a simple table view with a unique label for the cell prototype. I have been following the recommendations in order to have proper auto-sizing cells:
setting auto-layout constraints for the label regarding its leading, trailing, top and bottom spacing in its content view,
setting the rowHeight property to UITableViewAutomaticDimension,
providing the estimatedRowHeight property (set to 44),
setting the number of lines for the label to 0.
I also have enabled the default Edit Mode which allows the reordering and deletion of rows (exactly like in the Alarm tab of the Apple Clock app).
Now, on to observed problem:
I provide two labels, one with a short text and a second with a longer text, so that the second row has twice the height of the first row.
I enter Edit Mode, and swap the position of the rows. So far, everything keeps its composure and the sizing is still correct, the longer label being in the top row.
When I swap the positon again to return to the original layout (short label on top and long label on bottom), the longer text is not spread over two lines in its row as it should, but turns into a one-line display with an ellipsis at the end. Moreover, the height of the cell remains the same and does not fit the now single-lined text.
And after exitting Edit Mode (compare with the first image):
I suspect that I might be missing some kind of reload operation after the moving or deletion of rows, but I did not find the relevant information. Similar sizing problems appear when deleting rows that have been previously reordered.
How to keep the cell sizing as well as the text formatting consistent after moving/deletion of rows?
Thank you very much.

Swift, dynamic table view cell heights using Storyboards

I've been wrestling with this for a better part of a week and I'd love some help.
I have a cell laid out in storyboards that's a subclass of UITableViewCell called EntryCell. I've been using autolayout to set constraints in storyboards and all of the constraints on all the elements are blue, lldb throws no errors at runtime so nothing is duplicated or out of place.
My label that says "Journal Entry Here" below is a UILabel and has lines set to 0.
In my view controller under ViewDidLoad, I have
tableView.estimatedRowHeight = 185
tableView.rowHeight = UITableViewAutomaticDimension
but when I run that, simulator shows this:
This is holding up my entire project. I've read a number of tutorials and tried their implementations to no avail. I'm currently not doing anything with heightForRowAtIndexPath. I wonder if that's the problem or if it's something else.
Any help would be hugely appreciated. Thank you!
You shouldn't have to use heightForRowAtIndexPath to get this to work. The main reason UITableViewAutomaticDimension won't expand to fit unlimited line labels is because of missing layout constraints--constraints that won't necessarily show up in the console as errors. There must be continuous anchoring for every UI Object from the top to the bottom and to the left and right of your cell for expansion to work properly. If you think you've taken care of this and the cell still doesn't expand, try posting a screen shot and list of your layout constraints so we can review.

Resources