2013-03-03 26 views
0

我有一個帶有原型單元格的表格。我的項目使用自動佈局。按文本長度動態更改標籤和單元格大小

單元格包含一些標籤,它的文本長度可以是各種各樣的。有時它太長以至於不能適應其默認大小。

我想動態更改標籤/單元大小以顯示整個文本。如果需要,自動添加更多行。

我試過標籤的sizetofit,它根本沒有做任何事情。

回答

2

這是解決方案。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellText; 
    CGRect screenBound = [[UIScreen mainScreen] bounds]; 
    CGSize screenSize = screenBound.size; 
    CGFloat screenWidth = screenSize.width; 
    cellText = [detailPeriodsContent objectAtIndex:indexPath.row]; 
    UIFont *cellFont = [UIFont systemFontOfSize:14]; 
    CGSize constraintSize = CGSizeMake(screenWidth-40, MAXFLOAT); 
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 35; 
} 
相關問題