首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone UILabel sizeWithFont:

iPhone UILabel sizeWithFont:
EN

Stack Overflow用户
提问于 2009-07-30 01:23:16
回答 5查看 21.5K关注 0票数 5

我正在尝试测量NSString的视觉大小,其中考虑了我可以呈现的线条的数量。但是,sizeWithFont没有考虑numberOfLines属性吗?因此,我的布局算法将所有内容放置在比实际所需位置更低的位置。

代码语言:javascript
复制
_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来做这件事?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-10-06 05:38:48

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

代码语言:javascript
复制
[_price.text sizeWithFont:_price.font].height

然后乘以你想要的最大行数,然后将其插入到你限制自己的高度中。它可能看起来像这样:

代码语言:javascript
复制
_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。

票数 12
EN

Stack Overflow用户

发布于 2010-08-30 16:53:37

使用UILabel的sizeToFit而不是sizeWithFont:来布局多行UILabel,因为sizeWithFont:会截断字符串(参见苹果文档)。下面的代码减小标签的字体大小,直到文本适合指定的大小。当多行文本适合指定高度时,将立即使用它们:

代码语言:javascript
复制
-(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;
  } 
}

使用一个包含您最喜欢的文本和字体的标签来调用它:

代码语言:javascript
复制
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使用非常小的字体。

编辑:错误修复

票数 5
EN

Stack Overflow用户

发布于 2009-07-30 03:52:51

不要试图在一个调用中做到这一点,而是像这样做(请原谅伪代码,现在已经很晚了):

代码语言:javascript
复制
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

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1204000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档