ios7.0以前,使用sizeWithFont:constrainedToSize:lineBreakMode:来计算文本所占的高度或宽度:
如下例:
<span style="font-size:12px;">CGSize textSize = [callText sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:CGSizeMake(200, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping]; CGFloat textHeight = textSize.height;</span>
ios7.0后,sizeWithFont:constrainedToSize:lineBreakMode:被废弃,取而代之的是更为强大的boundingRectWithSize:options:attributes:context:
如下例子与上面例子效果一样:
<span style="font-size:12px;">NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0]}; CGSize textSize = [callText boundingRectWithSize:CGSizeMake(200, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size; CGFloat textHeight = textSize.height;</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/xiaokfc/article/details/46858621