UILabel自适应高度

    NSString *str =@"本店于十一期间特推出一系列优惠,限时限量敬请选购!<br>沙发:钻石品质,首领风范!<br>床垫:华贵典雅,彰显时尚!<br>尊贵而不失奢华,典雅却不失自然!温馨和浪漫的生活,我们与你一同创造!<br>";
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(10, 40, 300, 0)];
    UIFont *font =[UIFont systemFontOfSize:16];
    label.numberOfLines =0;
    label.text =str;
    label.font =font;
    [label sizeToFit];
    [mytable addSubview:label];

在heightForRow中

CGSize size2 =label.frame.size;

return size2.height+20;

而不是

return label.frame.size.height+20;


另一种方法

        contentStr =<span style="font-family: Arial, Helvetica, sans-serif;">@"本店于十一期间特推出一系列优惠,限时限量敬请选购!<br>沙发:钻石品质,首领风范!<br>床垫:华贵典雅,彰显时尚!<br>尊贵而不失奢华,典雅却不失自然!温馨和浪漫的生活,我们与你一同创造!<br>";</span>
        UIFont *font =[UIFont systemFontOfSize:16.f];
        NSDictionary *attributes =@{NSAttachmentAttributeName:font};
        CGSize size =CGSizeMake(self.view.frame.size.width-10, MAXFLOAT);
        CGSize labelSize =[contentStr boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        UILabel *textLabel =[[UILabel alloc]initWithFrame:CGRectZero];
        textLabel.frame =CGRectMake(5, 10, self.view.frame.size.width-10, labelSize.height +10);
        textLabel.numberOfLines =0;
        textLabel.text=[NSString stringWithFormat:@"%@",contentStr];
        textLabel.font = font;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textLabel.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textLabel.text length])];
        textLabel.attributedText = attributedString;
        [textLabel sizeToFit];
        [cell  addSubview:textLabel];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值