0

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!

User511
  • 1,389
  • 6
  • 25
Muju
  • 797
  • 13
  • 40

3 Answers3

2

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..

0

All you have to do is:

 float x=10.0,y=10.0,height=40;
 float verticalSpace=20.0;


for (int i = 0; i < 10; i++)
 {
    UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(x, y,50,height)];
    label.text = @"dummy text";// It could be from array as well if you want to add different text for each label. Eg:  label.text =  [arr objectAtIndex:i];
    [self.view addSubview:label];
    y = y +height+ verticalSpace;

 }
User511
  • 1,389
  • 6
  • 25
0

You could search around with key word "auto layout dynamic label height"

eric long
  • 464
  • 4
  • 13