簡體   English   中英

動態UILabel在iOS8中不起作用

[英]Dynamic UILabel not working in iOS8

我有一個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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM