2017-06-10 14 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我用砌體 –

相關問題