- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *dataStr = @"测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--测试自动换行--";
float height = [self heightOfContent:dataStr];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, height)];
[label setText:dataStr];
[label setLineBreakMode:NSLineBreakByCharWrapping];
[label setNumberOfLines:0];
[self.view addSubview:label];
}
// 根据内容计算文字高度
- (float)heightOfContent:(NSString *)content
{
UIFont *contentFont = [UIFont systemFontOfSize:14.f];
CGSize date1Size = [content sizeWithFont:contentFont constrainedToSize:CGSizeMake(275, 2000) lineBreakMode:NSLineBreakByCharWrapping];
float heightOfContent = MAX(25, date1Size.height );
return heightOfContent;
}