UILabel自适应高度

[cpp]  view plain copy
  1. UILabel *instructions=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 180)];  
  2.     NSString *text = @"这是个测试,不信你试试!这是个测试,再不信你copy代码!这是个测试,果然!自适应高度了!";  
  3.     instructions.text = text;  
  4.     instructions.lineBreakMode = NSLineBreakByWordWrapping; //这个是关键  
  5.     instructions.numberOfLines = 0;  //这个是关键  
  6.     [instructions setTextColor:[UIColor grayColor]];  
  7.     CGSize size = [instructions.text sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:CGSizeMake(300.0f,CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];  
  8.       
  9.     CGRect newFrame = instructions.frame;  
  10.       
  11.     newFrame.size.height = size.height;  
  12.     instructions.frame = newFrame;  
  13.     [instructions sizeToFit];  
  14.       
  15.     [self.view addSubview:instructions];  

你可能感兴趣的:(UILabel自适应高度)