UILabel的自适应 高度

来源:互联网 发布:淘宝收藏夹2000不够用 编辑:程序博客网 时间:2024/05/17 03:59

代码:

[plain] view plaincopy
 NSString* text=@"dwqqqqqqqqqqqqqqqqq";//初始化label     UILabel *comments = [[UILabel alloc] init];    comments.text = text;//设置自动行数与字符换行    [comments setNumberOfLines:0];    UIFont *font =[UIFont fontWithName:@"Helvetica" size:13];    comments.font = font;//设置一个行高上限     CGSize constraint = CGSizeMake(280, 20000.0f);    CGSize size = [text sizeWithFont:font constrainedToSize:constraint lineBreakMode:NSLineBreakByCharWrapping];//计算实际frame大小,并将label的frame变成实际大小     [comments setFrame:CGRectMake(5,indexPath.row==2?5:1, size.width, size.height+12)];    [image addSubview:comments];    [cell.contentView addSubview:comments];这样就可以实现自动行数和换行了。




  

0 0
原创粉丝点击