代码:
- //初始化label
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
- //设置自动行数与字符换行
- [label setNumberOfLines:0];
- label.lineBreakMode = UILineBreakModeWordWrap;
- // 测试字串
- NSString *s = @"这是一个测试!!!adsfsaf时发生发勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿啊00000000阿什顿。。。";
- UIFont *font = [UIFont fontWithName:@"Arial" size:12];
- //设置一个行高上限
- CGSize size = CGSizeMake(320,2000);
- //计算实际frame大小,并将label的frame变成实际大小
- CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
- [label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
这样就可以实现自动行数和换行了。
转自:http://blog.csdn.net/csj1987/article/details/6662852