How adjust label alignment using autolayout? - ios

I have some labels on my screen.4 Labels can have dynamic value it can display integer number value as well as it can also display some word as START.I have given the following constraints:
Leading to image
Top to image
Now as the text increase there is enough space on left so that label can expand it's width.but as i have given leading to top image so it will not move more to left.So please tell how can i move it to left when text increase.I know i can do it with the help of making outlet of leading constraint and change its value.
But is there any easy way of doing it ?

edit the leading Constraints to "less than equal to" '<=' and trailing fixed '=', whenever the label will have lengthier text its size will increase in the left side and be fixed on the right side

Related

Leading align behaviour?

I am giving leading align constraint for two label. When text in both the lebel is same both liked aligned properly. But if text is different they don't look aligned (Difference of 1 Pixel is coming), is there any constraint, that work independently of text.??

set label position with priority constraint?

I have one label to the right side of parent view.On top of label there is a view.I have given below constraints to label.
Align leading to top view (constant<16) LOW PRIORITY
Top space from parent view
Trailing space to parent view (constant=28) HIGH PRIORITY
I want when text inside the label increase it should change it's position.it should move towards left but at the same time it should also move towards right.I mean should increase it's width in both directions.As of now it's only moving towards left & keeping fix position from right.
Please tell how can i achieve this task?
i think if you set center alignment for label alignment you should get answer you want.Also give same priority to leading and trailing.
This will not work as the trailing space to super view constraint will most likely always win, as you have observed.
The width and height of the label is taken care of by its intrinsic content size and hence no constraints are needed for that. As far as position goes, the y is taken care of by the top space constraint. Finally the x will need a constraint that will allow it to grow in both directions as mentioned. To enable this behaviour remove the leading and trailing constraints and add a horizontally centered constraint. This will centre the label to its super views centre. To offset the position left or right you may choose a constant value or use the multiplier property.
The multiplier is good for positioning a certain percentage of the superview regardless of its size. e.g.: if the superview is the root view then a multiplier value of 0.5 places the label quarter of the way across the view, 1.0 makes the centre x align exactly, 1.5 makes it 3/4 across from the left.

UILabel Autoshrink Mal-use?

I Need a label in my UI to hold up to 32 characters without distorting, so I enabled auto shrink and changed the number of lines to 0:
heres my label on the IB:
now when I run the application it runs off the screen:
how can I make it fit accordingly?
You don't want the number of lines to be 0, because that will tell the label to use any number of lines that it needs. You also have to constrain the label's right edge to its superview's right edge, because without that, it'll run off the right side, as you can see.
remove constraints first
give the width 0 and apply width constraints . Click on the constraints change width constraint = to >= after which side you want (left or right) apply top space and (trailing space or leading space) apply two constraints.
clear the lable text.
try it and let me know after this wat is th problem.
You are on right direction. Set minimum font size and maximum font size of the label and not need to truncate characters, not need to set number of lines. Autoshrink definitely works.

Text alignment from bottom to top iOS SDK

As seen on the screenshot above, each top cell has few labels. The frame of the label this question concerning is highlighted with green rectangle. It shows the title of some picture/sculpture or other exhibit, so there could be quite long strings.
I would like the text of this label to be just above author label (string with font of smaller size). So, normal text is aligned from top to bottom and if text is not enough to fill all the space, the gap is at the bottom part of the label.
For this label I would like to use the opposite approach: if text is not enough, all the text at the bottom. Is there any parameter of UILabel to implement above mentioned behaviour? Or I should manually count the amount of lines and adjust the size of the label?
This is fairly easy using constraints. To get the effect you desire:
Constrain your label's position above the author label
Constrain the label's width or horizontal position
Constrain the label to a reasonable height using a greater than or equal to
Either constrain the label's total height as less than or equal to, or constrain the vertical position to something greater than or equal to.
All these constraints can be added in IB very easily, or through code. These constraints will cause the label to grow vertically to accommodate more text, and then shrink back down with less text, which will achieve the effect of having the text grow up from the bottom.

Autolayout - How to align with longest item

Suppose I have a view that contains two columns of objects. The first column contains labels, and the second column contains, say, sliders. I want the sliders all to align left with each other. I want the labels all to align left with each other. The trick is, how can I set the spacing between the labels and sliders so that the default spacing is used between the left sliders and the longest label, thus:
label 1 slider 1
label 2 slider 2
longer label 3 slider 3
very long label 4[]slider 4
label 5 slider 5
I've placed square brackets, [], where the default spacing should be. Is it possible to add this sort of constraint in the storyboard (iOS 7)? If so, how? The trick is that I don't know which label is the longest, especially with localization.
Ideally, I could specify a bounding box around the labels and align the sliders with the right side of the bounding box.
Yes, you can do this in IB. All the labels should have their left edges aligned, and one of them should have a fixed space to the left edge of their superview (all the elements should be enclosed in a UIView). Similarly, the sliders would have their right edges aligned and one of them would have a fixed space to the right edge of the box. Give one of the labels a horizontal spacing constraint to a slider -- make that a short one, say ==8 with a priority of 900 (that's the important thing). Now, give all the labels a >= constraint with whatever value you want for the minimum spacing (and leave the priority at 1000).

Resources