1

Sorry guys one simple doubt but i could not find any answer. My problem is i want to change the origin of element based on another elements. To be more correct for example if i have three labels, i set each labels in same x axis but different y axis one by one. if first label content increases second and third label's y axis should automatically increase. so how i can calculate it..

 UILabel *label1 =[[ UILabel alloc]init];
label1.frame=CGRectMake(10, 50, 10, 150);
label1.text=@"Favorite Cuisine ";

[ViewProfileResultSrollview addSubview: label1];

[label1 sizeToFit];
label1.textColor=[UIColor grayColor];

UILabel *label2 =[[ UILabel alloc]init];
label2.frame=CGRectMake(10, 80, 10, 150);
label2.text=@"Preferred Dress Style";

[ViewProfileResultSrollview addSubview: label2];

[label2 sizeToFit];
label2.textColor=[UIColor grayColor];

UILabel *label3 =[[ UILabel alloc]init];
label3.frame=CGRectMake(10, 120, 10, 150);
label3.text=@"Spoken Languages";

[ViewProfileResultSrollview addSubview: label3];

[label3 sizeToFit];
label3.textColor=[UIColor grayColor];
tshepang
  • 12,111
  • 21
  • 91
  • 136
IamDev
  • 299
  • 4
  • 17

2 Answers2

0

You could use the previous frame, since you are using [label1 sizeToFit];

Example below for label 2:

UILabel *label2 =[[ UILabel alloc]init];
label2.frame=CGRectMake(10, label1.frame.origin.y+label1.frame.size.height+30, 10, 150);
label2.text=@"Preferred Dress Style";
Architect
  • 133
  • 2
  • 8
  • k if i did not use size to fit am just displaying a paragraph in label by setting number of lines method now how i can calculate the origin – IamDev Aug 05 '13 at 12:46
  • Then you should use [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(label1.frame.size.width, 9999)]; this will get you the height of the text dependent on the label1's font and width. – Architect Aug 05 '13 at 12:54
  • My final question how i can restrict height of scrollview based on third label's originViewProfileResultSrollview.frame=CGRectMake(0, 0, 320,1000); [ViewProfileResultSrollview setContentSize:CGSizeMake(320, 3500)]; – IamDev Aug 05 '13 at 12:56
0

You can make it easy:

float        currHeight = 7.0;
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0.0, currHeight, self.view.frame.size.width, 0.0)];
[label setText: @"your text"];
[label sizeToFit];

CGRect labelFrame = label.frame;
labelFrame.size.height = label.contentSize.height;
label.frame            = labelFrame;

[self.view addSubview: label];

currHeight += label.frame.size.height + 5;

And you can use currHeight for your other labels!

Hope it will help!

user2545330
  • 408
  • 4
  • 17
  • k if i did not use size to fit am just displaying a paragraph in label by setting number of lines method now how i can calculate the origin – IamDev Aug 05 '13 at 12:54