usedRectForTextContainer for NSTextContainer 返回不正确的高度 [英] usedRectForTextContainer for NSTextContainer returning improper height
问题描述
我正在尝试使用以下代码获取在给定文本容器中显示文本所需的矩形大小:
I'm trying to get the size of the rectangle needed to display the text in a given text container with the following code:
s = [manager usedRectForTextContainer:container].size;
我希望文本在多行中,但它在一行的末尾被切断并且不显示下一行的内容.仅当有几个额外字符时才会发生这种情况;在大约 60 个字符的一行中,将有大约 10 或 15 个剩余的应该显示在下一行但没有显示的内容.
I expect the text to be in multiple lines, but it cuts off at the end of a line and doesn't show what is on the next line. This happens only when there are a few extra characters; in a line of about 60 characters, there will be about 10 or 15 leftover that should show on the next line but do not.
这可能是插图的问题吗?也许文字换行?我真的不确定,因为我是 iOS 和 UI 编程的新手.
Could this be a problem with insets? Perhaps text wrapping? I'm really not sure since I'm new to both iOS and UI programming.
在上面的代码中,manager是一个NSLayoutManager
,container是一个NSTextContainer
.
In the above code, manager is an NSLayoutManager
, and container is an NSTextContainer
.
我很确定这是输出的问题,因为在这个函数调用之后,如果我将 s 的值更改为我期望的值,我会得到显示整个字符串的预期布局.
I'm pretty sure the the output of this is the issue, because after this function call, if I change the value of s to what I expect it to be, I get the expected layout with the entire string shown.
关于布局管理器的信息是
The information about the Layout Manager is
<NSLayoutManager: 0xd203f60>
1 containers, text backing has 140 characters
Currently holding 140 glyphs.
Glyph tree contents: 140 characters, 140 glyphs, 1 nodes, 32 node bytes, 384 storage bytes, 416 total bytes, 2.97 bytes per character, 2.97 bytes per glyph
Layout tree contents: 140 characters, 140 glyphs, 62 laid glyphs, 2 laid line fragments, 3 nodes, 96 node bytes, 856 storage bytes, 952 total bytes, 6.80 bytes per character, 6.80 bytes per glyph, 31.00 laid glyphs per laid line fragment, 476.00 bytes per laid line fragment
我希望在这种情况下有 3 行,但看起来只有 2 行片段,这意味着问题可能出在我的布局管理器中?
I expect there to be three lines in this case, but it looks like there are only 2 line fragments, meaning perhaps the problem is in my layout manager?
推荐答案
您正在创建自己的文本容器,但您还需要对其进行配置.您需要设置要显示的 lineBreakMode 和 numberOfLines.
You are creating your own text container, but you also need to configure it. You need to set the lineBreakMode and the numberOfLines that you might want displayed.
就你而言,我很漂亮
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
这篇关于usedRectForTextContainer for NSTextContainer 返回不正确的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!