Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust UILabel on the content size with autolayout in iOS

I am trying to understand the basic of dynamic layout using the intrinsic content size. As attached in the image, i have two UILabel's in horizontal which will be my default layout. How do make the layout constraints so that

  • If the content size of either of two labels are greater than the other , it should be arranged in vertical stack
  • Also if either of two labels content size is greater than one line,how do we make the text grow satisfying the vertical arrangement

AutoLayout

like image 793
Nassif Avatar asked Nov 08 '22 09:11

Nassif


1 Answers

You can add two labels to stackView and if the sum of intrinsicContentSize of and check with the UIScreen.main.bounds.width minus the margins of left and right of stackView.

if (label1.intrinsicContentSize.width + label2.intrinsicContentSize.width) > (UIScreen.main.bounds.width - 48/* 48 is the left and right margins*/) {
     stackView.axis = .vertical
}else {
     stackView.axis = .horizontal
}

Remember to set the numberOfLines of labels to 0.

like image 89
Agent Smith Avatar answered Nov 11 '22 08:11

Agent Smith