UILabel如何在iOS7及以上版本中调整大小,sizeWithFont:constrainedToSize:lineBreakMode:已弃用

12
如果在iOS7中sizeWithFont:constrainedToSize:lineBreakMode:方法已被废弃,我该如何自动调整UILabel的高度和宽度以适应文本内容?

1
非常有兴趣了解这个。我尝试使用boundingRect,但是你不能像上面那样输入约束条件。 - Tim
boundingRectWithSize:options:attributes:context有什么问题吗? - peko
我一定是误用了它,我会再试一遍! - Tim
仍然无法让它正常工作,当我更新IBOutlet UILabel的框架时,尽管两个框架不同,但屏幕上什么也没有改变。 - Tim
这将无法与自动布局“原样”工作。在此处检查我的解决方案:https://dev59.com/umMl5IYBdhLWcg3wJD9S#18933978 - Deniss Fedotovs
2个回答

8

我最终使用了这个。对我有用。这不能与IBOutlets对象一起使用,但在动态计算uitableview的heightForRowAtIndexPath:方法中文本的高度时很有用。

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIFont fontWithName:@"FontName" size:15], NSFontAttributeName,
                                                            nil];

CGRect frame = [label.text boundingRectWithSize:CGSizeMake(263, 2000.0)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:attributesDictionary
                                                     context:nil];

CGSize size = frame.size;

这段代码在自动布局中不能“原样”使用。请查看我的解决方案:https://dev59.com/umMl5IYBdhLWcg3wJD9S#18933978 - Deniss Fedotovs

6
这应该适用于iOS6和iOS7,但会破坏您的标签约束(如果需要,您需要编写程序将它们全部重置):
-(void)resizeHeightForLabel: (UILabel*)label {
    label.numberOfLines = 0;
    UIView *superview = label.superview;
    [label removeFromSuperview];
    [label removeConstraints:label.constraints];
    CGRect labelFrame = label.frame;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect expectedFrame = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, 9999)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 label.font, NSFontAttributeName,
                                                                 nil]
                                                        context:nil];
        labelFrame.size = expectedFrame.size;
        labelFrame.size.height = ceil(labelFrame.size.height); //iOS7 is not rounding up to the nearest whole number
    } else {
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
        labelFrame.size = [label.text sizeWithFont:label.font
                                 constrainedToSize:CGSizeMake(label.frame.size.width, 9999)
                                     lineBreakMode:label.lineBreakMode];
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
    }
    label.frame = labelFrame;
    [superview addSubview:label];
}

将此方法添加到您的视图控制器中,并像这样使用它:
[self resizeHeightForLabel:myLabel];
//set new constraints here if needed

1
这真是救命稻草!我为了解决这么简单的问题而浪费的时间太多了。 - Dominic Williams
我正在使用这个,它可以很好地调整标签的大小,但是当标签重新添加到父视图时,位置会出现错误,尽管我可以验证原点在框架上仍然设置正确。 - Pete Martin
@PeteMartin 你确定 superview 中没有 UITableViewCell 或其他已缓存的 UI 元素吗?如果是这样的话,需要一些解决办法。 - Deniss Fedotovs
@DenissFedotovs 是的,它位于UITableViewCell的上层结构中。你知道有什么解决方法吗? - Pete Martin
在你的tableView:cellForRowAtIndexPath:函数中,创建一个临时的完全相同的duplicateLabel(宽度、字体和文本相同)在self.view上,通过使用上面的代码调整高度,使UITableViewCell框架内的标签具有相同的高度,然后从self.view中删除duplicateLabel。这应该就可以解决问题了。 - Deniss Fedotovs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接