繁体   English   中英

用于NSTextContainer的usedRectForTextContainer返回不正确的高度

[英]usedRectForTextContainer for NSTextContainer returning improper height

我正在尝试使用以下代码获取在给定文本容器中显示文本所需的矩形的大小:

s = [manager usedRectForTextContainer:container].size;

我希望文本在多行中,但是它会在一行的结尾处截断,并且不会显示下一行的内容。 仅当有几个额外的字符时才会发生这种情况。 在大约60个字符的行中,将在下一行显示大约10或15个剩余字符,但不显示。

插图可能有问题吗? 也许文字换行? 我真的不确定,因为我是iOS和UI编程的新手。

编辑:在上面的代码中,管理器是一个NSLayoutManager ,而容器是一个NSTextContainer

我很确定这是问题的输出,因为在此函数调用之后,如果将s的值更改为期望的值,则会得到包含整个字符串的期望布局。

有关布局管理器的信息是

<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行片段,这意味着问题可能出在我的布局管理器中?

您正在创建自己的文本容器,但还需要对其进行配置。 您需要设置lineBreakMode和可能要显示的numberOfLines。

在你的情况下我很漂亮

textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM