equal heights for different labels in one row - swift

I have 3 labels in a row they may have different number of words so each one may have different number of lines. I want at the end the height of views become the same. what should I do?

You want to use the Equal Heights constraint among your 3 labels. Below is an example. I have 3 labels in the same view, with the "lines" property of each label set to 0. Each has a top, leading, and width constraint applied (width = 100). So each label automatically grows taller as more text is added to them.
Now simply select all three labels and apply the Equal Heights constraint. Now two of the labels grow in height to match the third label. Now adding even more text to the label that has the most text will cause the other two labels to match its growing height. You can prove this directly within Interface Builder.

Related

Swift - why elements don't have equal sizes?

I have a Vertical Stack View on my screen (Alignment = Fill, Distribution = Fill).
In it there are from top to bottom:
Label (no constraints)
Vertical Stack View (height=21)
Horizontal Slider (height=21)
Label (no constraints)
I was expecting that both my labels (elements 1 &4) will be of equal size and will occupy the majority of the screen and 2nd & 4th elements therefore would be in the center of the screen. But in reality my bottom Label's height is sufficient only to fill it's text while my top Label takes the majority of the space.
I tried to change the Distribution of my Stack View to Fill Proportionally, but in that case my top label becomes smaller than the bottom one.
Why is it happening? How can I make my Labels to become of equal height?
PS I noticed a warning about Content Priority Ambiguity but I can't figure out what should I do about it
To make both labels equal in height I had to select both of them and add a constraint Equal Heights.
Warning about Content Priority Ambiguity was not about them, it was about horizontal labels. To remove warning while keeping their placement I changed Distribution of the horizontal Stack View to Equal Centering.

UILabel Minimum Number Of Lines

If you take a close look an an iMessage conversation cell, you’ll notice that the preview text is always two lines long. This can’t be a hard coded row height because the rows adjust to dynamic type. How can you always force a label to take up a certain number of lines even if there isn’t enough text to do so?
Set the label's numberOfLines to 2 and end the label's text with a linefeed \n. Set the wrapping to word wrap to prevent the ellipses from appearing.
This guarantees that the label text consists of at least 2 lines worth of material. Thus it can never be less than 2 lines, and since the maximum number of lines is 2, it can never be more than 2 lines. Thus it will always be (wait for it) 2 lines.
One way I think this could be possible is having a UIView as a parent of the UILabel.
Fix the height of the UIView based on device size class. Let's say for example 50 points for Width = Compact and Height = Regular.
Embed UILabel in UIView
Set number of Lines = 0 for UILabel
Now match UILabel leading , trailing ,top edge with the superview and leave the height as it is , also don't set the bottom constraints.
Select the superview i.e the UIView and UILabel together and select Equal Heights.
Open the constraint window and change label height less than or equal to SuperView height.
Short Message
Long Message
Height Constraint of the UILabel with respect to SuperView.

How do I adjust UILabel width according to text width?

I am making a todo list app. In one of the TableViewController, I have a stackview (with width 374) contains two labels: "name" and "*". It combines as "Name*". However, I see warning in Xcode saying that "width and horizontal position are ambiguous for "*" " and "width is ambiguous for "name" ".
Also, I can see there is a red line between these two labels and there is a big gap after the label "*". I think I can resolve it by adjusting the label width according to the text so that the stackview and also be smaller.
Is it possible to adjust the width of the UIlabel in Xcode. I have tried many ways but none of them is working.
I have a stackview (with width 374) contains two labels: "name" and "*"
That's your problem there. You have a stack view with a fixed width. I'll assume you use the default distribution property (i.e. fill). This creates ambiguity, Since this distribution value will try to give each arranged subview (i.e. each of your two labels) the width of its intrinsic content (i.e. the width of the text; what you really want). But you also set the stack view to be of fixed width (374). So, it doesn't know what label to "stretch" to fill the space (i.e. content hugging priority).
So, you're saying that you want to resize the stack view to wrap the labels. You only have to remove that fixed width constraint (374).
Alternatively, to make thing simple, you can only use one label, and set the content like Matt have said.

Storyboard labels constraints

I have 4 labels on my storyboard, aligned at baseline and with fixed distance between them.
Each label can have different values at runtime, and I would like that the group of these 4 labels is horizontally centered.
Didn't succeed to fix it !
I tried to put them in a view and center the view, but it doesn't work either.
Tried also to play with priorities, but didn't't succeed either...
Is it possible to achieve that in the storyboard ?
You can do this by putting your 4 labels inside of another view. Add the following constraints:
Set fixed distance constraints between the labels (3 constraints).
Set constraints to align the baselines of the labels (3 constraints).
Set a leading constraint from the leftmost label to the containing view. Set the constant to 0.
Set a trailing constraint from the rightmost label to the containing view. Set the constant to 0.
Set a constraint for the height of the view.
Set a constraint from the top of the left label to the top of its containing view.
Set a constraint to center the view horizontally.
Set a constraint to position the view vertically.
The width of the view will be determined by the intrinsic sizes of the labels plus the sizes of the distances in #1. This width will change as the contents of the labels changes, and the view will keep the group centered.
You can use a Stack View for this:
In the 'Attribute Inspector' you can set for example that each text field has an equal width and a spacing of 5 points between them:
You can then use your Stack View to center horizontally like your normally would do.

How to adjust multiple rows of labels in Xcode so that they occupy around 70 % of screen height?

I'm trying to build an app that has just a single portrait oriented view. There is a title on top and results label at the bottom of the view, these are locked to top and bottom and center, no problem here.
Now this is the tricky part. I have 9 rows of labels, most single row but couple span out to 2 rows. I want them to spread out evenly between the title and the result-label.
I lock the first label to top-left, lock the leading edges and then set equal vertical distance constraints between the labels. Otherwise ok but the last labels on 3.5 inch display overrun my results -label.
So I adjust either the fontsize or make vertical distance between labels smaller or both. 3.5 inch problem fixed but now there is a big empty gap between the last label and result. On iPhone 6 its really ugly.
I have tried all sorts of methods but can't just get this working.
So basically: how do I reserve 10 % of view height from top and 20 % from bottom and tell Xcode to use the rest 70 % (and all of it) for my 9 rows of label, evenly?
Thanks for help!
Would have posted an image but no rank for such things...
If I understand you right, you need several flexible spaces that will all be the same size. One space in-between each of the labels.
Create UIViews to put between each label, set them all to have equal widths or heights and anchor each one to the views it is supposed to sit between.
I got this idea from another SO question: Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5