动态计算行高

来源:互联网 发布:情义知多少 编辑:程序博客网 时间:2024/04/26 21:27
- (CGSize)currentSize{
   
CGFloat version = [[UIDevicecurrentDevice].systemVersionfloatValue];
   
//计算size  7之后有新的方法
   
CGSize size;
   
if (version>=7.0) {
       
//得到一个设置字体属性的字典
       
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:15],NSFontAttributeName,nil];
       
//optinos 前两个参数是匹配换行方式去计算,最后一个参数是匹配字体去计算
       
//attributes 传入使用的字体
       
//boundingRectWithSize 计算的范围
       
//_tweetBodystring
        size = [
_tweetBodyboundingRectWithSize:CGSizeMake(215,999)options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:diccontext:nil].size;
       
    }
else{
       
//ios7以前
       
//根据字号和限定范围还有换行方式计算字符串的size
       
//label中的fontlinebreak要与此一致
       
//CGSizeMake(215,999) 横向最大计算到215纵向max 999
        size = [
_tweetBodysizeWithFont:[UIFontsystemFontOfSize:15]constrainedToSize:CGSizeMake(215,999)lineBreakMode:NSLineBreakByCharWrapping];
    }
   
return size;
}
0 0