簡體   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