2017-06-10 16 views
0

我想以编程方式并排显示两个UILabel,如下图所示。 enter image description here如何在iOS中使两个UILabel并排编程方式

在图像中显示第一个标签是短值,但它的值是dyanmic,第二个UILabel超过图像中显示的两条线。但第二个标签设置为第一个UILabel的右侧。

有没有办法做到这一点编程中的方式..

我尝试很多事情要做到这一点,但没有什么是我帮助。

任何帮助被赞赏。

回答

1

试试这个:

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

result

+0

有没有办法在Xamarin的ios做到这一点? – Ironman

+0

你能告诉我你使用了哪个约束? – Ironman

+0

@Ironman我用砌体 –

相关问题