UILabel 自动换行 和支持换行符

作者: iOS 发布时间: 2021-04-06 浏览: 737 次 编辑

直接上代码:

NSString *str = @"你要放在label里的文本字符串  \n  换行符";
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(width,10000.0f)lineBreakMode:UILineBreakModeWordWrap];
 
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(x,y,size.width,size.height)];
lb.numberOfLines = 0; // 最关键的一句
lb.text = str;
lb.font = [UIFont systemFontOfSize:14];
[self.view addSubview:lb];
[lb release];