在iOS 7中动态调整标签大小

时间:2013-09-26 13:00:11

标签: objective-c uilabel ios7

在iOS 6中,我正在使用:

CGSize labelSize = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , labelSize.width, self.frame.size.height);

动态调整UILabel的大小。这在iOS 7中不起作用,所以我尝试了:

NSString *text = self.text;
CGFloat width = size.width;
UIFont *font = self.font;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
                                                                 attributes:@{ NSFontAttributeName: font }];

CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                              options:NSStringDrawingUsesDeviceMetrics
                              context:nil];
CGSize size = rect.size;

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , size.width, self.frame.size.height);

这是在UILabel的一个类别中,但这也不起作用...... 我应该使用什么想法?

4 个答案:

答案 0 :(得分:6)

尝试这样的事情(没有自动布局工作):

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;

答案 1 :(得分:3)

如果没有更多有关其无效的详细信息,我的猜测是您需要使用选项NSStringDrawingUsesLineFragmentOrigin才能成为旧sizeWithFont:的替代品,像这样:

NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
NSAttributedString *attributedText =
    [[NSAttributedString alloc]
        initWithString:text
        attributes:@
        {
            NSFontAttributeName: font
        }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
CGSize size = rect.size;

请注意文档提及:

  

在iOS 7及更高版本中,此方法返回小数(大小)   返回的CGRect的组件);使用返回的大小来确定大小   视图,必须使用将其值提高到最接近的更高整数   使用ceil函数。

因此,要拉出用于调整视图大小的计算高度或宽度,我会使用:

CGFloat height = ceilf(size.height);
CGFloat width  = ceilf(size.width);

答案 2 :(得分:3)

- (void)resizeLabelByContent:(UILabel *)label
{

    CGSize maxSize = CGSizeMake(label.width, 999);

    NSString *contentStr = label.text;

    UIFont *contentFont = label.font;

    CGRect contentFrame;

    NSString *version = [[UIDevice currentDevice] systemVersion];

    if ([version floatValue] < 7.0) {

        CGSize contentStringSize = [contentStr sizeWithFont:contentFont constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];

        contentFrame = CGRectMake(label.left, label.top, label.width, contentStringSize.height);

    } else {

        NSDictionary *contentDic = [NSDictionary dictionaryWithObjectsAndKeys:contentFont, NSFontAttributeName, nil];

        CGSize contentStrSize = [contentStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil].size;

        contentFrame = CGRectMake(label.left, label.top, label.width, contentStrSize.height);
    }

    label.frame = contentFrame;
}

答案 3 :(得分:1)

这应该适用于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];
}

将此方法添加到viewController并像这样使用它:

[self resizeHeightForLabel:myLabel];
//set new constraints here if needed