I have two label, L1 and L2 of variable and nonidentical length. I want to place these two label using constraints in Xcode. If possible i don\'t want to manipulate constraints
is your L1 and L2's width is fixed value or uncertain value ?
if the width is fixed, you can used Masonry to realization effect。
What you can do is put both the UILabel
inside a UIView
and then:
UILabel
.UILabel
.UILabel
Now center your UIView
horizontally and vertically in the middle of the screen. Set the horizontally in center constraint priority to 250. Add a leading and trailing space of your UIView
to its superView with a >=
5 constraint
Edit: Additionally if the text is too large then the above constraints aren't simply enough to work perfectly (You will have to set the numberOfLines to 0 and wrap mode to wordWrap to display the complete text and go to the next line, of course)! As the UILabel
s will have their inferred height and inferred width with a priority of 750 and the content compression resistance priority of each UILabel
is 750 by default too, Xcode will be confused what to do and rightfully so! What you can do is set the content compression property in attributes inspector to 1000
OR
If you don't wish to tinker with compression resistance (I used to find them scary), what you can also do is add a >=
constraint to the width and height both of each UILabel
. Giving this constraint will also work to show the complete text no matter how large it be, as explicitly defining height and width has given them a priority of 1000!