码迷,mamicode.com
首页 > 其他好文 > 详细

UILabel 根据文本内容设置frame

时间:2016-09-05 10:38:40      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

  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

标签:

原文地址:http://www.cnblogs.com/frounk/p/5841329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!