iOS Swift Multiline UILabel with StackView Inside UITableViewCell - ios

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"

Related

XCode: How To Adjust Items In A UIStackView

How do I adjust the TextField inside my StackView to fill in the gap between it and the label that I also have placed in there? I'm using the Storyboard.
Currently looks like this:
Desired look:
Thank you.
This is achievable by just using nested UIStackView.
Add a Horizontal UIStackView first with spacing as 15 and distribution and alignment as Fill.
Inside it add two vertical UIStackView with both set to fill equally as the distribution and alignment as Fill. The spacing can be set to 10.
Inside the first vertical one add two UILabel and inside the second one add two UITextField.
And this will result in:
The labels will expand to fit the largest label.

Two stacked dynamic height labels in UITableViewCell

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.

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.

Resources