2011-05-21 63 views
13

我有一個以編程方式編碼的UILabel。我想在按下按鈕時更改標籤的大小。如何更改該標籤的大小?這是我的代碼如何更改UILabel的大小

UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)]; 
[theLabel11 setText:@"US"]; 
[theLabel11 setTextAlignment:UITextAlignmentCenter]; 
[theLabel11 setFont: [UIFont fontWithName:@"Arial" size:13.0f]]; 
[theLabel11 setBackgroundColor:[UIColor orangeColor]]; 
[theLabel11 setTextColor:[UIColor blackColor]]; 
[scroll1 addSubview:theLabel11];  

回答

17

你應該將您的標籤聲明爲類屬性,因此可以從其他方法訪問

要更改fo NT大小使用

[theLabel11 setFont: [UIFont fontWithName:@"Arial" size:13.0f]]; 

要更改標籤的幀大小我們

theLabel11.frame = CGRectMake(x, y, width, height); 
11

調整上一個UIView空間信息的常見的成語是如下

label.frame = CGRectMake(
    x, 
    y, 
    width, 
    height 
); 

你可以得到舊的位置和高度通過

label.frame.origin.x 
label.frame.origin.y 
label.frame.size.width 
label.frame.size.height 
0

如果只有一個標籤添加到scroll1然後,重複滾動視圖,以獲取標籤的參考,如按鈕操作如下

for(UIView *subView in scroll1.subViews){ 

if([subView isKindOfClass:[UILabel class]]){ 
UILabel *lbl=(UILabel*)subView; 
//change size of label here 
} 
} 

如果有很多標籤,將標籤分配給每個標籤,同時創建和檢查,在for循環

+0

...或忘記該循環,並更好地使用viewWithTag: – Till 2011-05-21 08:56:39