Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios BoundingRectize返回错误高度_Ios_Objective C - Fatal编程技术网

Ios BoundingRectize返回错误高度

Ios BoundingRectize返回错误高度,ios,objective-c,Ios,Objective C,我编写了以下方法来计算具有给定宽度的NSAttribute字符串的高度: - (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue { CGFloat height = 0.0; if (text) { CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) optio

我编写了以下方法来计算具有给定宽度的NSAttribute字符串的高度:

- (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue {
    CGFloat height = 0.0;
    if (text) {
        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14 weight:UIFontWeightRegular] } context:nil];
        height = ceil(frame.size.height);
    }
    return height;
}
除颜色外,标签的属性字符串仅具有字体属性:

NSFontAttributeName:[UIFont systemFontOfSize:14权重:UIFontWeightRegular]

问题是,它偶尔会返回不正确的值。例如,我看到文本分为三行,但我得到的高度值是两行。类似地,有时它返回两行的高度值,而文本可以使用一行进行布局

我没有传递段落样式,因为NSLineBreakByWordWrapping是默认值


我读过类似的文章,但没有一篇给我答案。我遗漏了什么?

文本实际上是如何绘制的?