Krish Krish - 5 months ago 27
iOS Question

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

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"

Answer

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

Comments