设置两个动态Label之间的距离

时间:2014-04-05 12:08:32

标签: ios iphone objective-c

我想修复2 UILabel s之间的恒定垂直距离 当第一个UILabel调整大小时,下面的UILabel应动态调整Y位置。

这就是我现在所拥有的:

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];

由于

2 个答案:

答案 0 :(得分:2)

[myLabel sizeToFit](在您的情况下为 )会更改myLabel的高度参数,而myLabel2仍会在y = 200时启动(根据您的要求myLabel之后 因此,您还需要使框架设置动态化。

要解决Y的{​​{1}}职位问题,以下内容应该足够了:

myLabel2

答案 1 :(得分:0)

你必须为像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;