Two UILabels like UITableViewCellRightDetail using UIStackView - ios

I have a Horizontal UIStackView with two UILabels. I need it to be styled like UITableViewCellRightDetail.
The first Label (on the left) should have more priority in resizing.
Here is the example:
I need it to be like this:

You can just use right alignement for the second label to achieve the desired appearance on your second image.
However this will not affect size of the labels. If you want first label to fit as much as it can, you can define a width constraint and set it manually from code after you calculate it. The second label will just have leading and trailing constraints.

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.

iOS Constraints: how to give UILabel height constraint priorities

I have following problem:
I have a view with fixed height. In this view, there are two singleline UILables. The view is bigger than both of the labels so I want them to be displayed like this:
I added three constraints to achieve this:
first label top to view top
second label top to first label bottom
second label bottom to view bottom
(if you want to, i can add the code for this but I think its quite obvious)
So now the problem is following. Sometimes the view renders as intended, but somtimes it renders differently:
The behaviour is quite random.
I think it is because, both UILabels have a (autogenerated) height constraints with the same priority. And sometimes, the first label gets the correct size, sometimes the second label gets it.
So is there a way to change the priority of those autogenerated height constraints or another way to achieve this layout?
(I don't want to use a containerview for the second UILabel)
best regards
Your constraints are ambiguous, hence the random result. You haven't specified how the height should be used.
You should specify this by setting the vertical hugging priority of the top label to a value higher than the vertical hugging of the bottom label.

Make spacing between stackviews

I'm new to stackviews and have been trying to use it in a collectionViewCell. This works kind off however i would like to make spacing between the label and image and the two labels. Beside this i also want to make spacing between the label and the edges of the collectionViews. How can i achieve this? here is some images of my stackviews
i would like to make spacing between the label and image and the two labels
Select the vertical stack view right beneath ItemCell, then go to the attributes inspector and change the spacing value, as shown below.
Beside this i also want to make spacing between the label and the edges of the collectionViews.
You have to use constraints to achieve this.

Swift Center View Horizontal Ratio

I am creating a view within IB and and attempting to have 3 UILabels evenly space horizontally across the view. I came across this on SO, https://stackoverflow.com/a/30249550/4597666. I have three UILabels, each have the height and width constrained. Here is what the IB looks like:
I constrained each centered horizontally, and the first UILabel I have the multiplier 3:1, second, 3:3, third 3:5 like the post states.
When I run on my emulator, I don't get the result that I was expecting. It appears all three UILabels are centered horizontally, and the first and third are not offset.
Is there another setting that I'm missing, or another way to properly space the views evenly?
you need to make only one change.
Constraint you set is 3.CenterX to Superview.CenterX all you need to do is interchange the value so that you constraint should look like in below image.
Alternative solution. If you want to set constraints currently you have set then change the ratio from "3:5" to "5:3" and similar for all the labels.
Result:
Hope it helps you solving your problem.

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