UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0, 0, labelsize.width, labelsize.height)];
这样就可以实现自动行数和换行了。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0, 0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
- //初始化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)];
这样就可以实现自动行数和换行了。