星空网 > 软件开发 > 操作系统

【代码笔记】UILable高度自适应(sizeWithFont)

一,代码。

【代码笔记】UILable高度自适应(sizeWithFont)images/loading.gif' data-original="http://common.cnblogs.com/images/copycode.gif" />
- (void)viewDidLoad{  [super viewDidLoad];  // Do any additional setup after loading the view.    NSString *textString=@"1234567890";  NSLog(@"---height--%ld",(long)[self contentHeightWithText:textString]);}//根据要显示的text计算label高度- (NSInteger)contentHeightWithText:(NSString*)text{  NSInteger ch;  //size要和label中的size一样  UIFont *font = [UIFont fontWithName:@"Arial" size:11];  //300为宽,20000.0f为高。这个需要自己设定  CGSize size = CGSizeMake(300, 20000.0f);  //如果7.0以上版本。因为计算label的高度的函数变过。  if ([[UIDevice currentDevice].systemVersion doubleValue]>=7.0)  {    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];    size =[text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;  }  ch = size.height;  return ch;}
【代码笔记】UILable高度自适应(sizeWithFont)

 

二,输出。

2015-10-22 10:48:27.950 UILable高度自适应(sizeWithFont)[3232:93993] ---height--12

 




原标题:【代码笔记】UILable高度自适应(sizeWithFont)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流