如图所示,第一个标签是短值,但它的值是动态的,第二个 UILabel 多于两行,如图中所示。但是第二个标签设置为第一个 UILabel 的右侧。
有没有办法以编程方式做到这一点..
我尝试了很多方法来做到这一点,但对我没有任何帮助。
感谢任何帮助。
最佳答案
试试这个:
UILabel *label1 = [[UILabel alloc]init];
[self.view addSubview:label1];
label1.backgroundColor = [UIColor greenColor];
label1.font = [UIFont systemFontOfSize:15];
label1.text = @"ABC";
// this is the way
[label1 setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
UILabel *label2 = [[UILabel alloc]init];
[self.view addSubview:label2];
label2.font = [UIFont systemFontOfSize:15];
label2.text = @"label2label2label2label2label2label2label2label2label2label2label2";
label2.backgroundColor = [UIColor orangeColor];
label2.numberOfLines = 0;
[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(400);
make.height.mas_equalTo(18);
}];
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(label1.mas_right);
make.top.mas_equalTo(label1);
make.right.mas_offset(0);
}];
关于ios - 如何以编程方式在 iOS 中并排制作两个 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44472672/