cell 动态高度,UILabel 显示不完全出现...

- (CGFloat)contentCellHeightWithText:(NSString*)text width:(float)width font:(UIFont *)font
{
     CGSize size = CGSizeMake(width, MAXFLOAT);
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
        size =[text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
    return ceil(size.height);
}

因为不加ceil这个函数,就是准确的值,可能会出现空格等,导致宽度不一致的地方

而苹果的解释是
https://developer.apple.com/documentation/foundation/nsstring/1524729-boundingrectwithsize

To correctly draw and size multi-line text, pass [NSStringDrawingUsesLineFragmentOrigin](https://developer.apple.com/documentation/uikit/nsstringdrawingoptions/nsstringdrawinguseslinefragmentorigin) in the options parameter.

This method returns fractional sizes (in the size component of the returned [CGRect](https://developer.apple.com/documentation/corefoundation/cgrect)); to use a returned size to size views, you must raise its value to the nearest higher integer using the [ceil](https://developer.apple.com/documentation/kernel/1557272-ceil) function.

This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) are allowed to overlap the layout constraints specified by the size passed in, so in some cases the width value of the size component of the returned [CGRect](https://developer.apple.com/documentation/corefoundation/cgrect) can exceed the width value of the size parameter.


要正确绘制多行文本并调整其大小,请在 options 参数中传递 NSStringDrawingUsesLineFragmentOrigin。
此方法返回小数大小(在返回的 CGRect 的大小分量中);要使用返回的大小来调整视图大小,您必须使用 ceil 函数将其值提高到最接近的较高整数。
此方法返回字符串中字形的实际边界。某些字形(例如空格)允许与传入的大小指定的布局约束重叠,因此在某些情况下,返回的 CGRect 的大小组件的宽度值可能会超过大小参数的宽度值。

ceil其值提高到最接近的较高整数。

测试中,暂未出现异常...的情况

你可能感兴趣的:(cell 动态高度,UILabel 显示不完全出现...)