【问题标题】:iOS how to determine the size of UILabel dynamicallyiOS如何动态确定UILabel的大小
【发布时间】:2014-03-03 21:21:41
【问题描述】:

我正在尝试找出一种方法来根据字符串和字体大小了解 UILabel 的大小,以了解如何定义 CGRect 尺寸。

这是我所知道的:

UILabel *myLabel = [[UILabel alloc] init];
myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0];
myLabel.text = @"This is a string example"  

问题是我没有可能的字符串列表,但需要将标签居中,并且字符串不应该被截断(使用上面的示例:“这是一个字符串....”)。你们中的任何人都知道如何根据字符串和字体大小确定 UILabel 的大小吗?

非常感谢您的帮助。

【问题讨论】:

标签: ios iphone objective-c uilabel


【解决方案1】:

只需使用

 UILabel *myLabel = [[UILabel alloc] init];
 myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0];
 myLabel.text = @"This is a string example" 
 [myLabel sizeToFit]; //it will resize the label to just the text is set
 CGRectMake size = myLabel.frame; // u will get the frame of the label once is resized
 float height = size.size.height;  //to get the height of the label
 float width = size.size.width;  //to get the width of the label

希望对你有帮助

确保每次更改文本时调用 [myLabel sizeToFit];方法

【讨论】:

  • 我试过你的代码,但我得到 size.height 和 size.width 都等于 cero
【解决方案2】:

检查我的代码,我已经在我的一个应用程序中编写了它。

CGSize maximumLabelSize = CGSizeMake(270, FLT_MAX);
CGSize expectedLabelSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

NSLog(@"Expected Size of label based on string--%@",NSStringFromCGSize(expectedLabelSize));
//adjust the label the the new height.
CGRect newFrame = myLabel.frame;//get initial frame of your label
newFrame.size.height = expectedLabelSize.height;
myLabel.frame = newFrame;
NSLog(@"new frame of label--%@",NSStringFromCGRect(newFrame));
[myLabel setText:attString];

【讨论】:

  • sizeWithFont 已弃用, boundingRectWithSize:options:attributes:context 方法是更新的替代品。干杯
  • 谢谢,这段代码有点老了。我大约在 1 年前写了它来更新标签大小。
【解决方案3】:

刚刚遇到同样的问题。计算UILabels 或UITextViews 的大量大小很昂贵,所以我想知道是否有比配置标签或文本视图然后询问大小更便宜的方法。这在计算长表视图的行高时尤其重要。

原来,有:UILabel 似乎是 UIKit 为布局和绘图添加的字符串,而 UITextView 使用 Text Kit(在 iOS 7 中)。两种工具都可以配置为使用相同的设置,并且比使用 UIKit 类更便宜地计算字符串尺寸。

对于 UILabel,这是我发现的:

CGFloat labelHeight = [testString boundingRectWithSize:(CGSize){ labelWidth, CGFLOAT_MAX }
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:@{ NSFontAttributeName : labelFont };
                                               context:nil].size.height;
labelHeight = ceil(labelHeight);

假设numberOfLines = 0。注意需要对结果进行四舍五入。

我测试了上面的代码,将大约 4000 个字符串渲染成 100 种不同的宽度,所有结果都与我用于比较的 UILabel 相同。

我做了同样的事情来计算UITextView 的尺寸,这更复杂并且需要设置NSLayoutManager。这里的加速效果更令人印象深刻(比使用 UITextView 快约 50 倍)。

如果有人对此感兴趣,请联系code and test project

【讨论】:

    【解决方案4】:

    您可以通过以下方式计算标签的width

    [@"STRING" sizeWithFont:[UIFont fontWithName:@"FONT" size:12] forWidth:200 lineBreakMode:NSLineBreakByWordWrapping];
    

    否则你可以只设置标签中的字符串,然后调用sizeToFit,这将根据文本设置标签的frame

    【讨论】:

    • - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "使用 -boundingRectWithSize:options:attributes:context:") ;
    【解决方案5】:

    在 iOS7 中使用NSString 方法boundingRectWithSize:options:context:。看文档here

    CGRect rectYouNeed =
      [myLabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX)
                                 options:NSStringDrawingUsesLineFragmentOrigin
                                 attributes:@{ NSFontAttributeName : myLabel.font }
                                 context:nil];
    

    【讨论】:

      【解决方案6】:

      一定要看看这个问题Adjust UILabel height depending on the text

      似乎正是你要找的东西。

      【讨论】:

        【解决方案7】:

        我认为您需要的是多行,这将增加下面所需的区域。跟着这个就没有了……

        myLabel.lineBreakMode = NSLineBreakByWordWrapping;
        myLabel.numberOfLines = 0;
        

        【讨论】:

        • 但还是需要知道UILabel的尺寸
        猜你喜欢
        • 2015-03-03
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 2013-01-11
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多