//初始化label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
//設置自動行數與字符換行
[label setNumberOfLines:0];
label.lineBreakMode = UILineBreakModeWordWrap;
// 測試字串 NSString *s = @"這是一個測試!!!";
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)];