Muju Muju - 1 day ago 4
iOS Question

How to set UILabel height dynamically in Objective c?

I am new in iOS and I am facing problem regarding to create UILabel dynamically. I need to change its hight and the Y position according to data from webservice.
My code is like this

NSMutableArray *remarkarray =[[NSMutableArray alloc] init];
remarkarray=[responsedict valueForKey:@"Key"];
NSString *RemarkString=[NSString stringWithFormat:@"%@",[remarkarray objectAtIndex:0]];
remarklbl.text=RemarkString;
remarklbl.numberOfLines=0;
[remarklbl sizeToFit];


I have create UILabel like this

enter image description here

But its Y Position is fix. I need to create a UILabel in which I can change the height and the Y position according to text in it. Like this

enter image description here

Thanks in Advance!

Answer

You can do it very easily by declaring two labels in Storyboard.

Here are steps.

  1. Drag two UILabels in your viewController and select the first one and set constraints like below image.

enter image description here

  1. Select the second label and add following constraints.

enter image description here

  1. Select both labels and set numberOfLines to zero.

Now u can set text dynamically from code.

Adding text from code... Output here..

Comments