[英]Label Size to Fit inside UITableView
UILabel
的sizeToFit
方法不適用於cellForRowAtIndexPath
。
問題是我需要一些單元格看起來像這樣:
和其他人看起來像這樣:
有什么解決方案?
謝謝
在禁用情節提要中的自動布局后,sizeToFit方法將執行cellForRowAtIndexPath方法中應做的事情。
我遇到了同樣的問題,並以這種方式解決了:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIFont *font = [UIFont fontWithName:@"Roboto-Regular" size:14];
CGSize textSize = [[eventDetails objectForKey:[detailTitle objectAtIndex: indexPath.row]] sizeWithFont:font];
CGFloat strikeWidth = textSize.width;
if(strikeWidth <280) // if the length is lower than max width
{
return 30; // normal height of cell
}
else
{
return 30 + ((int) strikeWidth/280 )*15;// if not give the cell the height you want
}
}
並且不要忘記將標簽的行數設置為0 [label setNumberOfLines:0];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.