Reputation: 235
I want to fix a constant vertical distance between 2 UILabel
s.
When the first UILabel
resizes, the UILabel
below should dynamically adjust it's Y
position.
This is what I have right now:
CGRect labelFrame = CGRectMake(20, 20, 200, 200);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setBackgroundColor:[UIColor orangeColor]];
NSString *labelText = @"Floodgates. Floodgates. Keep 'em intact, gotta get home, gotta... gotta find... anywhere... mrph... just a few more blocks and I'll be there, and then this won't matter anymore... why didn't I just go before I left? I'm such an idiot. There's the hot dog vendor... past him, the bank... then another two blocks and I'll see apartments...";
[myLabel setText:labelText];
[myLabel setNumberOfLines:0];
[myLabel setFont:[UIFont boldSystemFontOfSize:12]];
[myLabel sizeToFit];
[self.view addSubview:myLabel];
CGRect labelsrame = CGRectMake(60, 200, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
[myLabel2 setBackgroundColor:[UIColor greenColor]];
NSString *label2Text = @"Floodgates. Floodgates. Keep 'em intact, gotta get home, gotta... gotta find... anywhere... mrph... just a few more blocks and I'll be there, and then this won't matter anymore... why didn't I just go before I left? I'm such an idiot. There's the hot dog vendor... past him, the bank... then another two blocks and I'll see apartments... almost home free, keep jogging, brisk... pace... hrm...";
[myLabel2 setText:label2Text];
[myLabel2 setNumberOfLines:0];
[myLabel2 setFont:[UIFont boldSystemFontOfSize:12]];
[myLabel2 sizeToFit];
[self.view addSubview:myLabel2];
Thanks
Upvotes: 1
Views: 928
Reputation: 1358
you have to create dynamic frame for both the label like bellow
UILabel *lblText1 = [[UILabel alloc]initWithFrame:CGRectMake(19, 250, 283, 20)];
lblText1.text = @"What ever your dynamic text";
CGSize maximumLabelSize = CGSizeMake(280,9999); // label with fix width 280
CGSize expectedLabelSize = [lblText1.text sizeWithFont:lblText1.font
constrainedToSize:maximumLabelSize
lineBreakMode:lblText1.lineBreakMode];
CGRect newFrame = lblText1.frame;
newFrame.size.height = expectedLabelSize.height;
lblText1.frame = newFrame;
// label 2
UILabel *lblText2 = [[UILabel alloc]initWithFrame:CGRectMake(19, 280, 283, 20)];
lblText2.text = @"What ever your dynamic text label 2";
CGSize maximumLabelSize1 = CGSizeMake(280,9999); // label with fix width 280
CGSize expectedLabelSize1 = [lblText2.text sizeWithFont:lblText2.font
constrainedToSize:maximumLabelSize1
lineBreakMode:lblText2.lineBreakMode];
CGRect newFrame1 = lblText2.frame;
newFrame1.size.height = expectedLabelSize1.height;
newFrame1.origin.y = lblText1.frame.origin.y + lblText1.frame.size.height + 10; // 10 for fix space between 2 label
lblText2.frame = newFrame1;
Upvotes: 0
Reputation: 20234
[myLabel sizeToFit]
(in your case) changes the height parameter of myLabel
while myLabel2
still starts at y=200 when it should start (as per your requirement) immediately after myLabel
.
Hence, you need to make your frame settings dynamic too.
To fix the Y
position issues for myLabel2
, the following should be sufficient:
//...
CGRect labelsrame = CGRectMake(60, myLabel.frame.origin.y + myLabel.frame.size.height, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
//...
Upvotes: 2