- UILabel *instructions=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 180)];
- NSString *text = @"这是个测试,不信你试试!这是个测试,再不信你copy代码!这是个测试,果然!自适应高度了!";
- instructions.text = text;
- instructions.lineBreakMode = NSLineBreakByWordWrapping; //这个是关键
- instructions.numberOfLines = 0; //这个是关键
- [instructions setTextColor:[UIColor grayColor]];
- CGSize size = [instructions.text sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:CGSizeMake(300.0f,CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
- CGRect newFrame = instructions.frame;
- newFrame.size.height = size.height;
- instructions.frame = newFrame;
- [instructions sizeToFit];
- [self.view addSubview:instructions];
UILabel自适应高度
最新推荐文章于 2024-06-19 15:52:51 发布