I have a parent UIView
where I place two labels on it. Each of these labels only has one line as can be seen here:
The problem now is that the baseline is wrong. I'm using auto layout and the question is how should my constraints should look like in this case? Especially the verticaly positioning of the labels. These are the constraints I currently have:
H:|-[title]-2-[description]-(>=5)-| //with NSLayoutFormatOptions.AlignAllFirstBaseline
V:|[title]|
V:|[description]|
The above constraints leads to
Unable to simultaneously satisfy constraints.
because the centering and the first baseline constraint are fighting each other. The labels should take the full height of the parent, but with different font size.
I tried to pin the labels to the top/bottom but that doesn't work for all cases.
How should I vertically position my labels?
Instead of using two different label for rich text you can use AttributedString. Here is a example:
In View did load you can set the string to label as below:
Output:
To see what happens, make the background of the label yellow. You have ambiguous constraints.
To fix it, remove the last vertical constraint. You don't need it.
This here works in my testing playground:
Result:
what you need to do is fairly simple. You two labels (the objects) just need to have the same size in height.
To do so add constraints to your first label (for exemple 40 px). When it's done elect the first AND the second label then in your contraints menu on the bottom of the screen (the one one the left, add new alignement constraint) select top edges and bottom edges.
Then you can set your width top bottom etc however you want.
Enjoy