计算文字高度

宽度固定 rect (UIDEVICE_SCREEN_WIDTH-25, MAXFLOAT)

-(CGSize) boundingALLRectWithSize:(NSString) txt Font:(UIFont) font Size:(CGSize) size{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:txt];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
[style setLineSpacing:2.0f];
[attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [txt length])];

CGSize realSize = CGSizeZero;

if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1

CGRect textRect = [txt boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:style} context:nil];
realSize = textRect.size;

else

realSize = [txt sizeWithFont:font constrainedToSize:size];

endif

realSize.width = ceilf(realSize.width);
realSize.height = ceilf(realSize.height);
return realSize;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容