为什么是sizeWithFont:constrainedToSize:lineBreakMode:返回不正确的大小? [英] Why is sizeWithFont:constrainedToSize:lineBreakMode: returning an incorrect size?

查看:118
本文介绍了为什么是sizeWithFont:constrainedToSize:lineBreakMode:返回不正确的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个可能的宽度为一个字符串我想显示在表格单元格的标签,我需要计算高度,以便表格单元格的高度被正确记录。然而,无论我为约束的大小,我得到相同的高度,这是不正确的,在我想要的情况。我使用的代码:

I have two possible widths for a string i want to display in a label in a table cell, and i need to compute the height so that the table cell's height is recorded correctly. however, no matter what I do for the constraining size i get the same height, which is incorrect in the case i want. The code i'm using:

CGFloat width = 300.0f;
NSString * value = @"LongText LongText LongText LongText LongText LongText";
CGSize contentSize = [value sizeWithFont: [UIFont systemFontOfSize: 14.0f]
                       constrainedToSize: CGSizeMake(width, CGFLOAT_MAX)
                           lineBreakMode: UILineBreakModeWordWrap];

当我检查contentSize变量时,宽度为252,高度为36。但如果不是300.0fi插入222.0f到宽度变量,宽度是189,但高度仍然是36,只有前4个LongText字显示在2行(第三行似乎被切断在某种程度上在计算)。有人知道为什么会发生这种情况吗?

When i inspect the contentSize variable, the width is 252 and the height is 36 which is expected. But if instead of 300.0f i plug in 222.0f into the width variable, the width is 189 but the height is still 36, and only the first 4 LongText words are displayed on 2 lines (the third line seems to be cut off somehow in the calculation). Does anyone know why this is happening?

推荐答案

查看此处此处看看您的代码中是否有任何异常。

Your code looks right. Look here and here to see if there is anything amiss in your code.

这篇关于为什么是sizeWithFont:constrainedToSize:lineBreakMode:返回不正确的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆