动态UILabel在iOS8中不工作

动态UILabel在iOS8中不工作,ios,objective-c,uilabel,Ios,Objective C,Uilabel,我有一个UILabel,可以接受长度可变的字符串。标签应展开以接受给定的任何长度的字符串。我在iOS7中有这样的功能,但在iOS8中,标签是单行的,文本太长时会被截断。行数设置为0。以下是我在iOS7中工作的代码: - (IBAction)btnClicked:(id)sender { [_theLabel setText:[_txtLabel text]]; CGSize constrainedSize = CGSizeMake(_theLabel.frame.size.w

我有一个UILabel,可以接受长度可变的字符串。标签应展开以接受给定的任何长度的字符串。我在iOS7中有这样的功能,但在iOS8中,标签是单行的,文本太长时会被截断。行数设置为0。以下是我在iOS7中工作的代码:

- (IBAction)btnClicked:(id)sender {

    [_theLabel setText:[_txtLabel text]];

    CGSize constrainedSize = CGSizeMake(_theLabel.frame.size.width, 9999);
    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0], NSFontAttributeName, nil];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[_txtLabel text] attributes:attributesDict];
    CGRect requireHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    if(requireHeight.size.width > _theLabel.frame.size.width){
        requireHeight = CGRectMake(0, 0, _theLabel.frame.size.width, requireHeight.size.height);
    }

    CGRect newFrame = _theLabel.frame;
    newFrame.size.height = requireHeight.size.height;
    [self.theLabel setFrame:newFrame];
}

对iOS8中发生的变化有什么建议吗?谢谢

我通常会这样做动态
UILabel
高度:

-(UILabel*)setupTitleLabelWithFrame:(CGRect)frame
{
    UILabel *lbl_title = [[UILabel alloc] initWithFrame:frame];

    lbl_title.textAlignment = NSTextAlignmentLeft;
    lbl_title.numberOfLines = 0;

    lbl_title.text = title;

    CGSize maximumLabelSize = CGSizeMake(lbl_title.frame.size.width, 9999);
    CGSize expectedSize = [lbl_title sizeThatFits:maximumLabelSize];
    lbl_title.frame = CGRectMake(lbl_title.frame.origin.x, lbl_title.frame.origin.y, lbl_title.frame.size.width, expectedSize.height);

    return lbl_title;
}

使用自动布局,无需任何代码行即可实现。设置帧后是否尝试调用
[label setNeedsLayout]
?感谢您的帖子,但是,此代码仅在iOS7或更低版本下有效。在iOS8中,标签是一行,文本被截断。我在iOS8中使用这段代码,没有任何问题