2011-12-08 73 views
0

我想知道是否有動態填充UIView的方式。在UIView中動態放置標籤和按鈕

例如,考慮將一些UILabel對齊爲行,其文本又是動態的。

如果第一次UILabel文本有多條線路,我知道我可以調用

[myLabel sizeToFit]

根據其內容調整其大小。

是否有一種簡單的方式將這些信息傳播到UIView的所有子視圖(UILabels)(我的情況是UILabels),並相應地製作它們並保留它們。

我希望我很清楚。

回答

1

對於應該按行排列的兩個標籤,可以使用CGRectGetMaxY函數。例如:

NSString *text = @"Text to display"; 
CGSize size = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:10.0f] constrainedToSize:CGSizeMake(100.0f, FLT_MAX) lineBreakMode:UILineBreakModeTailTruncation]; 
UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, size.height)]; 
[first setText:text]; 

UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f,20.0f, 100.0f, 200.0f)]; 
[first setText:@"Text to display"]; 
[first sizeToFit]; 
[self.view addSubview:first]; 
[first release]; 

UILabel *second = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(first.frame), 100.0f, 200.0f]; 
[second setText:@"Text to display"]; 
[second sizeToFit]; 
[self.view addSubview:second]; 
[second release]; 

UILabel *third = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(second.frame), 100.0f, 200.0f]; 
[third setText:@"Text to display"]; 
etc... 

您也可以根據其文字參數做這個知道標籤的預期大小