UILabel自适应高度

来源:互联网 发布:三表联查的sql语句 编辑:程序博客网 时间:2024/06/07 06:08
    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];


0 0
原创粉丝点击