以编程方式计算 UITableViewCell 高度包含“<br>”或“\n”的文本

Posted

技术标签:

【中文标题】以编程方式计算 UITableViewCell 高度包含“<br>”或“\\n”的文本【英文标题】:Calculate UITableViewCell height programmatically text containing "<br>" or "\n"以编程方式计算 UITableViewCell 高度包含“<br>”或“\n”的文本 【发布时间】:2012-04-26 06:04:07 【问题描述】:

我想使用代码计算单元格高度。我的文本包含 html 实体,如 &lt;br&gt;,&lt;b&gt; 等。我尝试了以下或类似的代码 -

//create a CGFloat variable
CGFloat _height = 0;
//find out the size for your text. Instead of 255 insert the width of your label
CGSize _textSize = [yourString sizeWithFont:[UIFont systemFontOfSize:kFontSize] constrainedToSize:(CGSize)  255, 9999 ];
//add the height of that CGSize variable to your height in case you will need to add more values
_height += _textSize.height;

它适用于单行代码和纯文本,但包含 html 标签的文本存在问题。

请帮忙。

【问题讨论】:

【参考方案1】:

试试这个与细胞高度相关的教程,并尝试一些谷歌搜索 http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

【讨论】:

我尝试了相同的代码,但正如我在问题中提到的那样,它适用于没有像 这样的 html 标签的文本,但 html 文本存在一些问题。 我有同样的问题...我的代码中有标记,\n 用于在单元格内容中创建中断。代码导致空间远大于内容的深度。 @ViscusAol 如果这个答案不能解决你的问题,为什么你把它标记为正确的?

以上是关于以编程方式计算 UITableViewCell 高度包含“<br>”或“\n”的文本的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式“触摸”UITableViewCell?

以编程方式将 UIImageViews 添加到 UITableViewCell

UITableViewCell 类布局无法以编程方式工作

在 Swift 中以编程方式突出显示 UITableViewCell

如何以编程方式使 UICollectionView 填充 UITableViewCell?

以编程方式在 UITableViewCell 上显示删除按钮