背景:
最近在忙身份证牌照识别,识别到的信息显示在一个UITableViewCell中,其中地址栏一般有20个汉字,一行显示不下,于是只能选择多行显示。
由于显示地址的label是动态添加到Cell中的,所以在判断中要指定到地址这一行。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == mReadTableView && indexPath.row == 4) //定位到身份证地址显示这行
{
// 列宽
CGFloat contentWidth = 200;//tableView.frame.size.width;
// 定义字体
UIFont *font = [UIFont systemFontOfSize:16];
// 該行要顯示的內容
NSString *content = mAddress; //mAddress即为需要分行显示的字符串
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 5000) lineBreakMode:UILineBreakModeWordWrap];
// 這裏返回需要的高度
return size.height>44?size.height:44;
}
return 44; //44为默认行高,其他行维持不变。
}
然后在下面函数中注意红色部分设置,如果直接用 cell.lab
if(tableView == mReadTableView && indexPath.row == 4) //定位到身份证地址显示这行
{
// 列宽
CGFloat contentWidth = 200;//tableView.frame.size.width;
// 定义字体
UIFont *font = [UIFont systemFontOfSize:16];
// 該行要顯示的內容
NSString *content = mAddress; //mAddress即为需要分行显示的字符串
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 5000) lineBreakMode:UILineBreakModeWordWrap];
// 這裏返回需要的高度
return size.height>44?size.height:44;
}
return 44; //44为默认行高,其他行维持不变。
}
然后在下面函数中注意红色部分设置,如果直接用 cell.lab