简体   繁体   中英

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;

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.

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.

Edit: In the above code, manager is an NSLayoutManager , and container is an NSTextContainer .

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

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?

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.

In your case I am pret

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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