在iOS7中使用了新的计算文本尺寸的方法替换之前的,文本尺寸的计算对于label、textView、cell自适高度很有帮助,因此在此记录:
NSString *text =@"abcdefghijklmnopqrstuvwxyz";
NSDictionary *attribute =@{NSFontAttributeName: [UIFont systemFontOfSize:28]};
CGSize size = [textboundingRectWithSize:CGSizeMake(50,0)
options:NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
attributes是关于所需要计算的字体的参数,因此需要与label(textView)中的字体相同。