Correctly wrap text in tableview cell - ios

I want to ask if anybody have solution for this kind of problem:
I have Tableview with dynamic cells, they have two labels and dynamic height. When the data is set to label, text in label is splitting in to new rows in the middle of word.
I tried set line breaking to word wrapping
In other tableviews with dynamic cell height I have no problem with splitting text
Labels have constraints to top, bottom, left and right of content view
Newest Xcode version and iOS > 13.0
Thanks for any help!

I download your demo and tried the same settings in English Language and it worked perfectly , so it seems a bug for me in Xcode for me ..

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

Autoshrink feature of Label in Tableview cell on scrolling doesn't work

I have two labels side by side in custom cell of tableview. When I am adding data into it first time it looks like this :
which is fine and is the required design .
Problem occurs when I start scrolling the tableview. After going down in tableview and coming up it changes like this:
First label has autoshrink feature added but not in second label. I am doing everything in storyboard except the data poulating part.
Please tell me if I am doing anything wrong in it.
Any help will be appreciated.
**EDIT: **
The custom cell that I have added in the project:
There are two labels that I have highlighted by selecting them.
Constraints for the First Label are:
I have also added Auto Shrink feature in first label :
Constraints for the second label are:
I haven't added Auto Shrink feature in second label as I have to keep fixed font size in second label.
Finally after reading more about Content Hugging Priority and Content Compression Resistance Priority I got the solution.
Thanks to this blog too:
https://krakendev.io/blog/autolayout-magic-like-harry-potter-but-real
I changed the priority as follows:
For First label:
For Second Label :
and it is working perfectly fine.

Two uilabels on uitable with dynamic height

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.

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.

Auto Layout, custom UITableViewCell, growing labels

I'm trying to solve this Auto Layout puzzle with a custom UITableViewCell which contains two labels that may grow vertically depending on their content i.e. number of lines. I've almost got it right, except that one label in one cell does not grow as expected! Please see screenshot where highlighted in red dashed line – the label does not end with a bracket, its text is clipped instead.
Please find the simple Xcode project here.
In my case there are two multiline labels and not just one as in other posts, and so there are 2 elements that effect the height of a cell. But regardlesss of that the soluion is to force the cell to do its layout.
So after calling dequeueReusableCellWithIdentifier() to get the cell, just add:
// fix for iOS lauout bug
cell.setNeedsLayout()
cell.layoutIfNeeded()
All starting and ending brackets are visible i.e. to text clipping :-)

Resources