我正在尝试测量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做到这一点?

有帮助吗?

解决方案

而不是CGFLOAT_MAX作为文本计算的最大高度,请尝试使用以下内容获取一行的大小:

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

然后将其乘以您想要的最大行数,然后将其插入您自己约束的大小的高度。它可能看起来像这样:

_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。

其他提示

使用UILabel的sizeToFit而不是sizeWithFont:来布局多行UILabel,因为sizeWithFont:将截断字符串(请参阅apple docs)。以下代码减少了标签的字体大小,直到文本符合指定的大小...一旦符合指定的高度,将使用多行文本:

-(void)setFontSizeOfMultiLineLabel: (UILabel*)label 
        toFitSize: (CGSize) size 
        forMaxFontSize: (CGFloat) maxFontSize 
        andMinFontSize: (CGFloat) minFontSize 
        startCharacterWrapAtSize: (CGFloat)characterWrapSize{

CGRect constraintSize = CGRectMake(0, 0, size.width, 0);
label.frame = constraintSize;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0; // allow any number of lines

for (int i = maxFontSize; i > minFontSize; i--) {

    if((i < characterWrapSize) && (label.lineBreakMode == UILineBreakModeWordWrap)){
        // start over again with lineBreakeMode set to character wrap 
        i = maxFontSize;
        label.lineBreakMode = UILineBreakModeCharacterWrap;
    }

    label.font = [label.font fontWithSize:i];
    [label sizeToFit];
    if(label.frame.size.height < size.height){
        break;
    }       
    label.frame = constraintSize;
  } 
}

使用您喜欢的文字和字体的标签来调用它:

UILabel *label = [[UILabel alloc] initWithFrame: CGRectZero];   
label.backgroundColor = [UIColor clearColor];   
label.textColor = [UIColor whiteColor];
label.text = text;
label.font = [UIFont fontWithName: @"Helvetica" size: 16];
[self setFontSizeOfMultiLineLabel: label toFitSize: CGSizeMake(200, 44) forMaxFontSize: 16 andMinFontSize: 8 startCharacterWrapAtSize: 11]; 

startCharacterWrapAtSize参数允许您选择从给定字体大小开始使用characterWrap。如果wordWrap使用非常小的字体,这应该可以节省空间。

编辑:bugfix

不要试图在一次通话中这样做,而是做这样的事情(原谅伪代码,已经晚了):

NSString *s = _price.text;
UIFont *font = _price.font;
CGFloat fontSize = font.pointSize;

while (TRUE)
{
   CGSize priceSize = [s sizeWithFont: font constrainedToSize: 
       CGSizeMake(maxWidth, fontSize) lineBreakMode: UILineBreakModeWordWrap];

    if ( /* priceSize is satisfactory */ )
    {
        break; // Make sure this exits, eventually!!!
    }
    fontSize -= 1.0; // or a smaller decrement if you like
    font = // new, smaller font

}

当然,正确的答案是你需要将numberOfLines设置为0,这将导致框架计算结果,但需要很多行。另请参见此问题

当然,它没有考虑到这一点,因为没有任何被调用或传入的信息。您正在严格使用字符串,大小和字体。它是标签中包含行数。

我不确定你的问题究竟是什么;你的尺码是太高还是太短,或者是什么?你可以通过将结果的高度除以字体的高度来找出文本行数,我相信这是ascender和descender的值。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top