UITableViewCell 的动态高度计算依赖于 TextView 的高度 [英] Dynamic Height Calculation of UITableViewCell depend upon TextView's height

查看:31
本文介绍了UITableViewCell 的动态高度计算依赖于 TextView 的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableview 的内容中给出了 textview 的前导和尾随.我想动态计算Tableviewcell的高度.高度计算有时可以正常工作,但有时无法正常工作,例如 5c.

I have given the leading and trailing to textview inside content of tableview . I want to dynamically calculate the height of Tableviewcell. the height calculation is working fine sometimes but sometimes not working particular devices like 5c.

我使用以下代码来估计文本的高度:

i have used following code to estimate the height of text :

func estimateFrameForText(text: String) -> CGRect {
    let size = CGSize(width: self.view.frame.size.width, height: 10000000)
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
    return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSFontAttributeName: UIFont.init(name: FontStyle.FontName.kDefault, size: 15.0)!], context: nil)
}

这里重要的是文本是 HTML 文本,我已经将其转换为纯文本.然后将纯文本传递给上述函数.

The important thing here is the text is HTML text, I have converted it into plain text. then that plain text is passed to above functions.

推荐答案

  1. 为表格视图单元格内容中的文本视图提供顶部、底部、前导和尾随.
  2. 禁用文本视图的滚动.然后,这种方法有效.它通常用于聊天单元格,其中我们想要自动检测链接,电话号码减去uitextview的滚动的数据的属性.
  3. 使用以下表格视图的方法设置自动行高计算

  1. Give top,bottom,leading and trailing to textview inside content of tableview cell.
  2. Disable the scrolling of textview. Then and then, this method works. It is generally used in chat cells, wherein we want the property of automatic Data detection of links, phone numbers minus the scrolling of uitextview.
  3. Set automatic row height calculation using below methods of table view

func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat
{
   return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat

{
   return 100
}

这篇关于UITableViewCell 的动态高度计算依赖于 TextView 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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