UILabel的sizeToFit / sizeThatFits忽略numberoflines属性 [英] UILabel's sizeToFit/sizeThatFits ignore the numberoflines property

查看:537
本文介绍了UILabel的sizeToFit / sizeThatFits忽略numberoflines属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:确定UILabel需要的大小(行数),假设宽度为300像素。字符串更长,所以我将lineBreakMode设置为UILineBreakModeWordWrap并调用sizeThatFits尝试确定大小。但它在一行中的宽度为457像素,而不是两行中的预期300像素。

Problem: Determine the size (number of lines) a UILabel needs, assuming the width is 300 px. The string is longer, so I set the lineBreakMode to UILineBreakModeWordWrap and invoked sizeThatFits to try to determine the size. But it gives a width of 457 px in a single line, rather than the expected 300px in two lines.

请参阅:

CGSize available = CGSizeMake(300, INFINITY);
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 400)] autorelease];
label.text = title;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:kBoldFont size:kTitleFontSize];
label.numberOfLines = 3;
CGSize sizedtoFit = [label sizeThatFits:available];

但我发现sizestoFit变量的宽度为457像素,高度为22像素, UI将显示带有剪辑文本的单行。我预计宽度为300像素,两行的高度为44像素。

But I find that the sizedtoFit variable has a width of 457 pixels and a height of 22 px, and the UI displays a single line with clipped text. I expect a width of 300 pixels, and a height of 44 px for two lines.

numberoflines的UILabel文档说:

The UILabel doc for numberoflines says:

当使用sizeToFit方法调整接收器大小时,调整大小会考虑存储在此属性中的值。例如,如果此属性设置为3,则sizeToFit方法会调整接收器的大小,使其大小足以显示三行文本。

When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

我尝试了各种组合:


  1. 将CGRectZero传递给init函数,传递300x400或300x无限大。


  2. 调用[sizeToFit]并希望它计算高度,假设当前宽度,但不是。

  3. 调用sizeToFit然后调用sizeThatFits。

  4. 调用layoutIfNeeded。

  1. Passing CGRectZero to the init function, passing 300x400 or 300 x infinity.
  2. Setting the frame after creation rather than passing it to the init function.
  3. Invoking [sizeToFit] and hoping it calculates the height assuming present width, but it doesn't.
  4. Calling sizeToFit and then calling sizeThatFits.
  5. Invoking layoutIfNeeded.

他们工作。我做错了什么,或者这是坏的bug,文档和框架实现不同意?感谢。

None of them works. What am I doing wrong, or is this is bad bug where the documentation and the framework implementation don't agree? Thanks.

推荐答案

我有同样的问题,大小适合完全忽略大小... /:
最后使用:

I had the same problem, size that fits simply ignores the size... /: I ended up using:

CGRect textSize = [UILabel textRectForBounds:CGRectMake(0, 0, 300, CGFLOAT_MAX) 
                      limitedToNumberOfLines:3];

像一个charm ...:)

Works like a charm... :)

文档说,你不应该直接调用它,但我已经使用了一段时间,已批准提交的应用程序,一切都是真棒...:)

The documentation says you shouldn't call it directly, but i've been using it for a while, with approved submitted apps, and everything is just awesome... :)

这篇关于UILabel的sizeToFit / sizeThatFits忽略numberoflines属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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