计算 UITableViewCell 高度以适合字符串 [英] Calculate UITableViewCell height to fit string
本文介绍了计算 UITableViewCell 高度以适合字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 NSMutableArray
,它可以包含数百个不同的字符串.每个字符串都是用户定义的,可以有任意长度,但不超过平均段落.
I have a NSMutableArray
, which could contain several hundreds of different strings. Each string is user defined and could have any length, yet no more than an average paragraph.
我需要找出每个字符串可以占用多少行.因此,我可以计算每个 UITableviewCell
所需的高度.
I need to find out how many lines each string could take. So, I could calculate the required height for a every UITableviewCell
.
推荐答案
使用以下代码计算和设置单元格高度:
Use the following code to calculate and to set the height for cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Define `veryverySmallTitleFont`
// Define `descLabel`
NSString *ourText = @"Your string data from array";
UIFont *font = [UIFont fontWithName:@"Verdana" size:veryverySmallTitleFont];
font = [font fontWithSize:veryverySmallTitleFont];
CGSize constraintSize = CGSizeMake(descLabel.frame.size.width, 1000);
CGSize labelSize = [ourText sizeWithFont:font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height;
}
这篇关于计算 UITableViewCell 高度以适合字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文