2012-10-01 59 views
0

我有以下代碼來調整標籤的高度,但它不起作用。 它顯示的文字..... 我做錯了什麼?UILabel調整高度不起作用

float storeAddress1YOffset=10; 
float storeAddress1XWidth=scrollView.frame.size.width; 
float storeAddressLabel1YHeight=15; 
UILabel *storeAddressLabel=[[UILabel alloc]initWithFrame:CGRectMake(Xoffset,storeSectionHeight+ storeAddress1YOffset,storeAddress1XWidth, storeAddressLabel1YHeight) ]; 
storeAddressLabel.font=[UIFont fontWithName:@"GillSans" size:15.0f]; 
currentHeight=currentHeight + storeAddress1YOffset + storeAddressLabel1YHeight; 
[storeAddressLabel setText:fullAddress]; 

CGRect labelFrame = storeAddressLabel.frame; 
labelFrame.size = [fullAddress sizeWithFont:storeAddressLabel.font 
          constrainedToSize:CGSizeMake(storeAddressLabel.frame.size.width, CGFLOAT_MAX) 
           lineBreakMode:storeAddressLabel.lineBreakMode]; 
storeAddressLabel.frame = labelFrame; 

回答

1

您的代碼似乎適用於我。有幾個變量,你沒有實例:

float Xoffset = ?; 
float storeSectionHeight = ?; 
float currentHeight = ?; 
NSString *fullAddress = @"?"; 

除了你不顯示有關scrollView或添加storeAddressLabel來(我假設)scrollView作爲一個子視圖的任何細節。

當我使用普通的UIView填充這些缺失的部分時,標籤似乎正確調整爲測試字符串fullAddress。請包含一些更多的代碼,包括這些缺失的部分。

+0

我通過在初始化幀標籤之前確定幀大小來了解它的工作原理。 – user1688346