iPhone UILabel sizeWithFont: [英] iPhone UILabel sizeWithFont:

查看:102
本文介绍了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);
///后面的代码忽略了numberOfLines,只是告诉我整个块的大小。
//我想让它知道numberOfLines
//
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth,CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];

有谁知道如何使用iPhone SDK做到这一点?

解决方案

而不是CGFLOAT_MAX文本计算的最大高度,请尝试获取一行的大小:

  [_ price.text sizeWithFont:_price.font] .height 



然后将其乘以所需的最大行数,然后将其插入到所限制的大小的高度。它可能看起来像这样:

$ p $ _price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
CGFloat lineHeight = [_price.text sizeWithFont:_price.font] .height;
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth,lineHeight * _price.numberOfLines)
lineBreakMode:UILineBreakModeWordWrap];

如果您将行数设置为0,则最大高度将为0在这种情况下;那么您应该使用CGFLOAT_MAX。


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?

解决方案

Instead of CGFLOAT_MAX for the max height of your text calculation, try getting the size of one line with this:

[_price.text sizeWithFont:_price.font].height

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:

_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
CGFloat lineHeight = [_price.text sizeWithFont:_price.font].height;
CGSize priceSize = [_price.text sizeWithFont:_price.font
        constrainedToSize:CGSizeMake(maxWidth, lineHeight * _price.numberOfLines)
        lineBreakMode:UILineBreakModeWordWrap];

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.

这篇关于iPhone UILabel sizeWithFont:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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