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

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

概述我一直在尝试使用 swift做一个带有标签的动态单元格来支持heightForRowAtIndexPath中的 IOS7,但我只找到了objective-c代码,是否有可能帮助我将这段代码重写为swift? // Fetch yourText for this row from your data source.. NSString *yourText = [yourArray objec 我一直在尝试使用 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 = someTextlabel.numberOflines = 100label.Font = UIFont(name: "Times New Roman",size: 19.0)label.sizetoFit()return label.frame.height + 10
总结

以上是内存溢出为你收集整理的ios – 动态自定义UITableViewCell的高度基于Swift中的标签文本长度全部内容,希望文章能够帮你解决ios – 动态自定义UITableViewCell的高度基于Swift中的标签文本长度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107620.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存