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)];
[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)];
這樣就可以實現自動行數和換行了。