2

I want to display two UILabel's, however UILabel's having variable text length's According to textsize UIlabel's width need to increase using auto-layouts.

For this I wrote below auto-layouts for both UIlabel's

First Label:

1)leading Space 
2)Top space
3)Width
4)height
5)Horizontal spacing

Second Label:

1)Trailing space
2)Top space
3)Width
4)height

how can we do this ?

Please help me.

my code:

textLabel1.numberOfLines = 0
        textLabel1 .sizeToFit()
        textLabel1.text = "asdfdsfdghjgjhkhkjlhjkhjk"

textLabel2.numberOfLines = 0
        textLabel2 .sizeToFit()
        textLabel2.text = "asdfdsfdghjgjhkhkjlhjkhjk"
Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
Krish
  • 4,166
  • 11
  • 58
  • 110

2 Answers2

5

Select the First UILabel, set Leading, Top, bottom and set fixed width, then go to size Inspector -> select fixed width constraints -> Relation -> select Greater than or equal, example is give below,

enter image description here

enter image description here

select the Second UILabel, set Leading, Bottom, fixed width, then above same processs to be perform,

hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • Greater Than or Equal means if your label width is 42, if you set greater than or equal to extend full screen width based upon your text, its extent larger width >= 42, but its fixed 42 and above only. – Iyyappan Ravi Jun 14 '16 at 05:09
  • Less Than or Equal means if your label width is 240 and is fixed, if you set less than or equal, 240 is maximum value but your text is small, its automatically change the width size based upon your text. if your text above 240 its not visible full text, – Iyyappan Ravi Jun 14 '16 at 05:14
  • Less Than or Equal example is chatting App, Whats App, any Chatting check it. ok you understand and above constraints working or not – Iyyappan Ravi Jun 14 '16 at 05:15
  • hi i am having one small dought u thre? – Krish Jun 14 '16 at 06:11
  • Tell me what is your dought? – Iyyappan Ravi Jun 14 '16 at 06:13
  • As i said above i have two labels that's all-right if i given trailing space for UIlabel 2 then width and height both are increasing based on Lable's text size is it possible? – Krish Jun 14 '16 at 06:18
  • i hope u understand my dought? – Krish Jun 14 '16 at 06:18
  • If you give trailing space, its have some issues, if you give long text its ok, but if you give two word text its allocate full width of the screen, check set background color in `uilabel`. you understand my answer? – Iyyappan Ravi Jun 14 '16 at 06:34
  • No i can't see this link or image – Iyyappan Ravi Jun 14 '16 at 06:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114587/discussion-between-krish-and-iyyappan-ravi). – Krish Jun 14 '16 at 06:42
0

Fist you need to set UILabel's property numberOfLines to zero and set text wrapping to word wrap. And after that give the required constraint.

If you are not getting desired result then you need to calculate the label's dynamic height by boundingRectWithSize:... method and give height constraint accordingly.

Mahendra
  • 8,448
  • 3
  • 33
  • 56