首页 > 代码库 > UILabel 根据文本内容设置frame

UILabel 根据文本内容设置frame

  CGRect senderFrame = cell.senderLabel.frame;
    CGRect creatAtFrame = cell.creatAtLabel.frame;
    CGSize senderSize = [cell.senderLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:15.0]}];
    senderFrame.size.width = ceil(senderSize.width);
    if (senderFrame.size.width>0) {
        senderFrame.size.width=ceil(senderSize.width)+10;
    }
    
    if (senderFrame.size.width>200) {
        senderFrame.size.width = 200;
    }
    
    cell.senderLabel.frame = senderFrame;
    creatAtFrame.origin.x = CGRectGetMaxX(cell.senderLabel.frame)+10;
    cell.creatAtLabel.frame = creatAtFrame;
    
    NSDictionary *info = messageModel.info;
    // 设置属性来设置行高
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.0],NSParagraphStyleAttributeName:paragraphStyle};
    CGFloat width = CGRectGetWidth(self.view.frame)-CGRectGetMinX(cell.senderLabel.frame) - 16;
    CGRect frame = cell.descLabel.frame;
    frame.size.width = width;
    
    cell.descLabel.emojiDelegate = self;
    [cell.descLabel sizeToFit];
    
    //    cell.descLabel.text = info[@"message"];
    [cell.descLabel setEmojiText:messageModel.timeViewStr];
    
    CGRect bodyFrame = CGRectMake(0, 0, 0, 0);
    //    if (cell.descLabel.text!=NULL) {
    bodyFrame = [cell.descLabel.text boundingRectWithSize:CGSizeMake(width, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];

    frame.size.height = ceil(bodyFrame.size.height);
    cell.descLabel.frame = frame;

UILabel 根据文本内容设置frame