【问题标题】: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 标签的文本存在问题。

请帮忙。

【问题讨论】:

    标签: iphone ios uitableview row-height


    【解决方案1】:

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

    【讨论】:

    • 我尝试了相同的代码,但正如我在问题中提到的那样,它适用于没有像
      这样的 html 标签的文本,但 html 文本存在一些问题。
    • 我有同样的问题...我的代码中有标记,\n 用于在单元格内容中创建中断。代码导致空间远大于内容的深度。
    • @ViscusAol 如果这个答案不能解决你的问题,为什么你把它标记为正确的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多