ios – 根据UITableView Swift中的文本大小扩展标签单元格大小

ios – 根据UITableView Swift中的文本大小扩展标签单元格大小,第1张

概述我是iOS开发的新手.我的细胞大小有问题.我没有使用Auto-Laylout功能.我当前的TableView单元看起来像 like this.我想使 image中选择的标签大小根据该Label的文本内容动态扩展. 提前致谢. 我认为像下面的快速版本,它计算更接近的值,而不是精确的高度,例如 class ViewController: UIViewController,UITableViewData 我是iOS开发的新手.我的细胞大小有问题.我没有使用auto-Laylout功能.我当前的tableVIEw单元看起来像 like this.我想使 image中选择的标签大小根据该Label的文本内容动态扩展.

提前致谢.

解决方法 我认为像下面的快速版本,它计算更接近的值,而不是精确的高度,例如
class VIEwController: UIVIEwController,UItableVIEwDataSource,UItableVIEwDelegate { var messageArray:[String] = [] overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    // Do any additional setup after loading the vIEw,typically from a nib.      messageArray = ["One of the most interesting features of Newsstand is that once an asset downloading has started it will continue even if the application is suspended (that is: not running but still in memory) or it is terminated. Of course during while your app is suspended it will not receive any status update but it will be woken up in the background","In case that app has been terminated while downloading was in progress,the situation is different. Infact in the event of a finished downloading the app can not be simply woken up and the connection delegate finish download method called,as when an app is terminated its App delegate object doesn’t exist anymore. In such case the system will @R_325_4404@ the app in the background."," If defined,this key will contain the array of all asset IDentifIErs that caused the launch. From my tests it doesn’t seem this check is really required if you reconnect the pending downloading as explained in the next paragraph.","Whale&W","Rcokey","Punch","See & dive"]}

在上面我们有一个包含不同长度字符串的数组

func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int    {        return 1;    }func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int{    return messageArray.count;}func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell{    var cell:UItableVIEwCell? = tableVIEw.dequeueReusableCellWithIDentifIEr("CELL") as? UItableVIEwCell;    if(cell == nil)    {        cell = UItableVIEwCell(style:UItableVIEwCellStyle.default,reuseIDentifIEr: "CELL")        cell?.selectionStyle = UItableVIEwCellSelectionStyle.none    }    cell?.textLabel.Font = UIFont.systemFontOfSize(15.0)    cell?.textLabel.sizetoFit()    cell?.textLabel.text = messageArray[indexPath.row]    cell?.textLabel.numberOflines = 0    return cell!;}func tableVIEw(tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGfloat{    var height:CGfloat = self.calculateHeightForString(messageArray[indexPath.row])    return height + 70.0}func calculateHeight(inString:String) -> CGfloat{    let messageString = inString    let attributes : [String : Any] = [NSFontAttributename : UIFont.systemFont(ofSize: 15.0)]    let attributedString : NSAttributedString = NSAttributedString(string: messageString,attributes: attributes)    let rect : CGRect = attributedString.boundingRect(with: CGSize(wIDth: 222.0,height: CGfloat.greatestFiniteMagnitude),options: .useslineFragmentOrigin,context: nil)    let requredSize:CGRect = rect    return requredSize.height}

在新项目中尝试它只需添加tablevIEw并设置其数据源并委托并通过上面的代码并运行

结果如下

总结

以上是内存溢出为你收集整理的ios – 根据UITableView Swift中的文本大小扩展标签/单元格大小全部内容,希望文章能够帮你解决ios – 根据UITableView Swift中的文本大小扩展标签/单元格大小所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存