码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UILable高度自适应

时间:2015-04-24 21:01:26      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

适用于iOS6以后

NSString *tip = @"UILable高度自适应,UILable高度自适应,UILable高度自适应";

UILabel label_2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 200, 30)];
label_2.font = [UIFont systemFontOfSize:15];
label_2.lineBreakMode = NSLineBreakByCharWrapping;
// 设置行数
label_2.numberOfLines = 0;      
[label_2 setText:tip];
[self.view addSubview:label_2];
    
// 计算高度
CGFloat height = [tip sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(100, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping].height;
    
// 重新改变label_2的frame
CGRect rect = label_2.frame;
rect.size.height = height;
label_2.frame = rect;

label_2.lineBreakMode =NSLineBreakByCharWrapping;//其中lineBreakMode可选值为

linBreakMode enum{
  NSLineBreakByWordWrapping
= 0,  //保留整个单词,以空格为边界
  NSLineBreakByCharWrapping,    //保留整个字符   NSLineBreakByClipping,      //以边界为止   NSLineBreakByTruncatingHead, //省略开头,以省略号代替   NSLineBreakByTruncatingTail, //省略结尾,以省略号代替   NSLineBreakByTruncatingMiddle //省略中间,以省略号代替 }

 

iOS UILable高度自适应

标签:

原文地址:http://www.cnblogs.com/wb145230/p/4454350.html

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