Constrain text to a location, then have it dynamically move if required - ios

I am making a UITableView with two labels. I have constrained the secondLabel to be +12 from the firstLabel, meaning it will move along if the firstLabel contains a lot of text. However, that means if the firstLabel contains hardly any text the secondLabel will move back. What can I do to make this work?
I would like to have it react like this:
secondLabel stays in place despite there being very little text in the firstLabel:
_________________________________________________________
| |
| [abc] [secondLabel] |
|________________________________________________________|
secondLabel moves along due to firstLabel having a lot of text to display:
__________________________________________________________
| |
| [extralongfirstLabel] [secondLabel] |
|________________________________________________________|
Both the firstLabel and secondLabel will have a varying amount of text. The firstLabel leading side should be constrained to the SuperView margin.
The secondLabel would maintain a distance of 12 from the leading side of the firstLabel, but then remain 56.5 from the SuperView when not constraining to the firstLabel (when it contains little text).
Both text labels are on the left of the cell, allowing the text to spread across it.
I am trying to complete this task via the IB, but if you have any code suggestions please let me know.

This sounds like job for multiple constraints, one of which has a lower priority.
First, a required (priority 1000) greater-than-or-equal-to constraint to space the second label's leading edge 12 points from the first label's trailing edge.
Second, a lower-priority constraint for the distance between the cell's and the second label's leading edges. This will be broken when the first label becomes too large, which is fine: that's why it's lower priority.
When the second constraint is broken, the first constraint will be the control for the distance between the labels.

So, for first label set its leading constraint equal to superview's with constant how you want to.
Now, for second label set two leading constraints.
One equal to first label's trailing constraint with some static
constant (means relation equal) and with lower priority (set its priority lower than for the second leading constraint, so for example 999)
Second equal to superview's leading constraint with
relation greater than or equal
Constraints:
Result:

Related

Alternative labels giving Fixed width constraints may cause clipping warning why?

I have designed two labels side by side i have given left side label 150 fixed width, why alternatively getting below warning.
For above image left label i have given constant text and constraints like below
fixed width = 150, top = 10, leading = 10 , height = 30
for right label text will come from json and constraints like below
top = 10, leading = 10, trailing = 10, height = 30
i have highlighted all my left side fixed width labels, here for all labels i got below warning
Main.storyboard Fixed width constraints may cause clipping
but here if i select first label warning and if give greater then or equal and horizontal hugging from 251 to 250 then warning disappears.. like same if i do for second label then it becomes big like below
for third label if i give greater then or equal then warning disappears.. like alternatively warning disappears why, you can see that in top image. why its happening like this, how to clear all my warnings. please help me.
1.Give the left sided labels horizontal content hugging priority of 252.
2.Give the right sided labels horizontal content hugging priority of 251.
3.Give horizontal spacing of 8 between the left sided and right sided labels.
4.Remove fixed widths for all labels.
5.Align leading and trailing of all the left sided labels and just the trailing of all the right sided labels.
6.Give vertical space to left sided label from right sided label of the previous row.
7.Vertically center align the left sided and right sided label in the same row.
All your warnings will disappear.

Autolayout minimum gap between UIStackViews

I have 3 UIStackViews laid as follows edge to edge:
SV1 ---- SV2 ---- SV3
SV1 sits at a fixed distance of 5 points to superview leading, SV2 center is aligned with center of superview, and SV3 is at a fixed distance of 5 points to views trailing. Problem is on iPhone SE, SV2 is too wide and too close to SV1 and SV3. How do I set autolayout constraints so that SV2 is at a minimum distance D to SV1 and SV3?
You can create two constraints: first between sv2 and sv1 and second between sv2 and sv3. The trick is to set the constraint not equals to a a value but greater than.
You can create horizontal spacing constraints between the stack views and set them to greater-or-equal. This takes care of the minimum space between them.
But since they were to close together this means at least one of them has to get smaller to make space for the separation. You control which one by setting the content compression resistance priority of the views inside your stack views. The one with the lowest value shrinks.
If all three stack views have different widths you also could lower the priority of the center constraint to a value below the spacing constraints priority. That would mean that the center view is pushed to one side to make room for the spacing.
There are lots of possibilities that depend on your exact views. The best way is to experiment a bit with the priorities.

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.

How adjust label alignment using autolayout?

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

Resources