gpt4 book ai didi

ios - 设置两个动态标签之间的距离

转载 作者:行者123 更新时间:2023-11-29 10:44:27 26 4
gpt4 key购买 nike

我想固定 2 个 UILabel 之间的恒定垂直距离。
当第一个 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];

谢谢

最佳答案

[myLabel sizeToFit](在您的情况下)更改 myLabel 的高度参数,而 myLabel2 仍然启动在 y=200 时它应该在 myLabel 之后立即开始(根据您的要求)。
因此,您也需要使框架设置动态化。

要修复 myLabel2Y 位置问题,以下内容应该足够了:

//...
CGRect labelsrame = CGRectMake(60, myLabel.frame.origin.y + myLabel.frame.size.height, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
//...

关于ios - 设置两个动态标签之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com