计算UITableViewCell高度以适合字符串 [英] Calculate UITableViewCell height to fit string

查看:33
本文介绍了计算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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆