Krish Krish - 2 years ago 147
iOS Question

How to change UILabel width based on text length in iOS Using Auto-Layouts

I want to display two

's, however
's having variable text length's According to textsize
's width need to increase using auto-layouts.

For this I wrote below auto-layouts for both

First Label:

1)leading Space
2)Top space
5)Horizontal spacing

Second Label:

1)Trailing space
2)Top space

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"

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download