计算UILabel / UITableViewCell的多行文本高度:计算与实际绘图时的结果不同 [英] Calculating multiline text height for UILabel/UITableViewCell: different results when calculating vs actual drawing

查看:240
本文介绍了计算UILabel / UITableViewCell的多行文本高度:计算与实际绘图时的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里已经多次询问了这个一般性主题:如何使用不同数量的文本渲染UITableViewCells,从而改变高度。规范的答案是:使用sizeWithFont:constrainedToSize:lineBreakMode:在heightForRowAtIndexPath中计算表视图控制器委托中的高度。之后,单元格会被绘制出来,如果需要你可以使用像[label sizeToFit]这样的东西,而且所有东西都像魔法一样。

This general topic has been asked here multiple times: how to render UITableViewCells with varying amount of text and thus varying height. The canonical answer is: you calculate the height in table view controller delegate in heightForRowAtIndexPath using sizeWithFont:constrainedToSize:lineBreakMode:. Later, the cell gets drawn, and you use something like [label sizeToFit] if needed, and all works like magic.

我的问题:我正在为一些单元格进行包装因为sizeWithFont:从实际绘图中返回不同的尺寸。

My problem: I am getting wrapping for some cells because sizeWithFont: returns different dimensions from actual drawing.

一个具体的例子:

文本如下:人们忘记了@BillGates在1993年从NEC获得了性感的1/4英寸厚的板岩。本周发生的任何事情都不会与硬件有关!

The text is this: "People forget that @BillGates had a sexy 1/4-inch thick slate back in 1993 from NEC. Whatever happens this week will NOT be about hardware!"

CGSize theSize = [text sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310.0f, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"calculated size for %@: %f, %f",text, theSize.width, theSize.height);

返回:306.000000,84.000000。 (即4行,17px字体和4px行间距,21px前导。)好。

This returns: 306.000000, 84.000000. (I.e 4 rows with 17px font and 4px linespacing, 21px leading.) Good.

但是,稍后实际绘制单元格时:

However, later when actually drawing the cell:

label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:@"%@", text];
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:17.0f];
CGSize labelSize;   
labelSize = label.frame.size;
NSLog(@"label size before resizing: %f, %f", labelSize.width, labelSize.height);
[label sizeToFit];
labelSize = label.frame.size;
NSLog(@"label size after resizing: %f, %f for text %@", labelSize.width, labelSize.height,text);

(UILabel是从NIB加载为UITableViewCell的一部分。在IB中我将其设置为310px宽。)

(UILabel is loaded as part of UITableViewCell from NIB. In IB I set it to 310px wide.)

这应该返回与上面完全相同的大小。相反,我得到281.000000,10500.000000作为sizeToFit调用后的维度。它现在是绘制时间的5行而不是4行,文本溢出,我看到UI中的溢出效应。

This should return exactly the same size as above. Instead, I get 281.000000, 105.000000 as the dimensions after sizeToFit call. It is now 5 lines at drawing time instead of 4, and the text spills over, I see the spillover in the UI.

所以,对于相同的文字,我是计算出两个不同的维度,无法弄明白。它是关于UILabel的吗?它有一些内在的边缘吗?对于某些文本而言,这种情况不断发生,而其他文本却没有发生,我没有将其追溯到字符串的特定内容;似乎随意。 本主题强调有两种处理方式通过:计算高度与实际绘图。这与我所看到的一致。但我不明白究竟发生了什么或如何解决它。

So, for the same text, I am getting two different dimensions calculated, and can't figure it out. Is it something about UILabel? Does it have some inner margins? This keeps happening for some texts but not others, and I have not traced it to something particular about the strings; seems random. This topic highlights that there are two processing passes: calculating height vs actual drawing. This is consistent with what I'm seeing. But I don't understand what exactly is going on or how to fix it.

问题:为什么我看到两种不同的计算尺寸,我该如何解决它?

The question: why am I seeing two different calculated sizes, and how do I fix it?

推荐答案

当然,解决方案在发布后显然 30 秒。也许对其他人有用...

Of course, the solution is obvious 30 seconds after posting. Maybe useful to others too...

sizeWithFont:的大小是正确的。我以上述方式计算的尺寸不正确,因为 [label sizeToFit] 会减小标签框架的宽度。在后续调用相同的代码时,它从可能已经减少的帧开始。

The size by sizeWithFont: was correct. The sizes I calculated in the above way were incorrect, because [label sizeToFit] reduces the width of the label's frame. At subsequent calls to the same code, it started off with the frame that may already have been reduced.

修复只是重置帧宽度在调整大小之前到已知的良好宽度:

The fix was to simply reset the frame width to a known good width before sizing to fit:

CGRect labelFrame = label.frame;
labelFrame.size.width = 310;
label.frame = labelFrame;
[label sizeToFit];

这篇关于计算UILabel / UITableViewCell的多行文本高度:计算与实际绘图时的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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