iPhone UILabel sizeWithFont:
我正在尝试测量 NSString 的视觉大小,其中考虑了我可以渲染的行数。 但是, sizeWithFont 不考虑 numberOfLines 属性吗? 因此,我的布局算法将所有内容放置在低于实际需要的位置。
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
/// the follow code ignores numberOfLines and just tells me the size of the whole block.
// I'd like it to be aware of numberOfLines
//
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
有谁知道如何使用 iPhone SDK 来做到这一点?
I'm trying to measure the visual size of a NSString that takes into account the number of lines I can render. However, sizeWithFont doesn't take into account numberOfLines property? So my layout algorithm positions everything lower than they actually need to be.
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
/// the follow code ignores numberOfLines and just tells me the size of the whole block.
// I'd like it to be aware of numberOfLines
//
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
Does anyone know how to do this using the iPhone SDK?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要使用 CGFLOAT_MAX 作为文本计算的最大高度,而是尝试使用以下方法获取一行的大小:
然后将其乘以所需的最大行数,然后将其插入到您限制自己的大小的高度中。 它可能看起来像这样:
如果您将行数设置为 0,请不要使用此选项,因为在这种情况下您的最大高度将为 0; 那么你应该使用 CGFLOAT_MAX 。
Instead of CGFLOAT_MAX for the max height of your text calculation, try getting the size of one line with this:
and then multiplying that by the maximum # of lines you want, then plugging that into the height of the size you are constraining yourself to. It'd probably look like this:
Don't use this if you ever set number of lines to 0 as your max height will be 0 in that case; you should use CGFLOAT_MAX then.
使用 UILabel 的 sizeToFit 而不是 sizeWithFont: 来布局多行 UILabel,因为 sizeWithFont: 会截断字符串(请参阅苹果文档)。 以下代码减小标签的字体大小,直到文本适合指定的大小...多行文本将在适合指定的高度后立即使用:使用
包含您喜欢的文本的标签来调用此代码font:
startCharacterWrapAtSize 参数允许您选择从给定的字体大小开始使用characterWrap。 在 wordWrap 使用非常小的字体的情况下,这应该可以节省空间。
编辑:错误修复
Use the UILabel's sizeToFit instead of sizeWithFont: to layout a multi-line UILabel, since sizeWithFont: will truncate the string (see apple docs). The following code reduces the font size of a label until the text fit into a the specified size... multiple lines of text will be used as soon as they fit into the specified height:
Call this with a label that has your favorite text and font:
The startCharacterWrapAtSize parameter lets you choose to use characterWrap starting at the giving font size. This should save space in the case wordWrap would use really small fonts.
edit: bugfix
不要尝试在一次调用中完成此操作,而是执行以下操作(请原谅伪代码,已经晚了):
Instead of trying to do it in one call, do something like this (pardon the pseudocode, it's late):
正确的答案当然是,您需要将 numberOfLines 设置为 0,这将导致框架使用所需的行数来计算结果。 另请参阅此问题。
The correct answer is, of course, you need to set numberOfLines to 0, which will cause the framework to compute the result with however many lines it needs. See also this question.
当然,它不会考虑到这一点,因为调用或传入的任何内容都没有该信息。 您严格使用字符串、大小和字体。 它是包含行数的标签。
我不确定你的问题到底是什么; 您购买的尺码是否太高或太矮,或者是什么? 您可以通过将结果的高度除以字体的高度来找出文本的行数,我相信这是上升部分加上下降部分的值。
Of course it doesn't take it into account, since nothing being called or passed in has that information. You're strictly working with strings, sizes, and fonts. It's the label that has the number of lines in it.
I'm not sure what exactly your problem is; are you getting a size that's too tall or too short, or what? You can find out the number of lines of text by dividing the height of the result by the height of the font, which is the value of the ascender plus the descender, I believe.