一种方法是,你给你的文字指定固定行数
lab.numberOfLines = 3;
lab.lineBreakMode = NSLineBreakByCharWrapping;
另一种方法是
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [text boundingRectWithSize:CGSizeMake(width MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;
}
其中attributes 内容代表你显示的文字字体大小,width代表你一行的宽度,在上面的方法中,返回rect.size.height 这样就可以根据文字换行了