Muju Muju - 1 year ago 128
Objective-C Question

How to add UILabel 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 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 Source

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

Here are steps..

  1. Drag two UILabels in your viewController and select first one and set constraints like below image. Adding Constraint tow first label

  2. Select second label and add following constraints.

Adding Constraint to second label

  1. Select both label and set the numberofline to zero.

Now u can set text dynamically from code.. [adding text from code][3] [output here...][4]

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