ios – 动态自定义UITableViewCell的高度基于Swift中的标签文本长度

我一直在尝试使用 swift一个带有标签的动态单元格来支持heightForRowAtIndexPath中的 IOS7,但我只找到了objective-c代码,是否有可能帮助我将这段代码重写为swift?
// Fetch yourText for this row from your data source..
    Nsstring *yourText = [yourArray objectAtIndex:indexPath.row];

    CGSize lableWidth = CGSizeMake(300,CGFLOAT_MAX); // 300 is fixed width of label. You can change this value
    CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:19] constrainedToSize:lableWidth lineBreakMode:NSLineBreakByWordWrapping]; // You can put your desire font

    // Here,you will have to use this requiredSize and based on that,adjust height of your cell. I have added 10 on total required height of label and it will have 5 pixels of padding on top and bottom. You can change this too.
    int calculatedHeight = requiredSize.height+10;
    return (float)calculatedHeight;

正是这句话(如何将其转换为swift):

CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:19] constrainedToSize:lableWidth lineBreakMode:NSLineBreakByWordWrapping]

我尝试将其转换为(但它不能作为原始目标-c):

var requiredSize: CGSize = originalString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(19.0)])

解决方法

也许有可能在没有UILabel的情况下做到这一点,但这很好用.
let label = UILabel(frame: CGRect(x: 0,y: 0,width: 300,height: 10000))
label.text = someText
label.numberOfLines = 100
label.font = UIFont(name: "Times New Roman",size: 19.0)
label.sizetoFit()
return label.frame.height + 10

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...