Skip navigation.
 
mlCalculate height of text in a text field
FROM : pavao
DATE : Mon Nov 22 23:12:27 2004

Hi,

I need to calculate the vertical space occupied by the text in a given NSTextField so that I can expand it vertically accordingly. To do that, I'm creating my own private instances of NSTextContainer, NSLayoutManager and NSTextStorage. I connect them to each other and then use -[NSLayoutManager usedRectForTextContainer:] to get the height of the text. The problem is that the calculated value is not always right and either I get one extra empty line or I end up with the last line of text not being visible because the TextField wasn't resized to a big enough value.

I've searched the net and the list archives and added the following lines of code prior to calling usedRectForTextContainer:

   [textContainer setLineFragmentPadding:2.0];
   [layoutManager setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];

This supposedly mimics the typesetting behavior used by the text fields. However, the calculated height value still isn't consistent with the layout done by the text field! After some testing, I came to the conclusion that using "NSTypesetterOriginalBehavior" instead of the suggested "NSTypesetterBehavior_10_2_WithCompatibility" gives me the desired behavior. Everything shows up fine! However, as was previously mentioned on this list (and also on the Apple docs, I guess) the correct value to use should be "NSTypesetterBehavior_10_2_WithCompatibility".

I'm building and testing under MacOS X 10.3.6.

Am I doing something wrong? Can I use "NSTypesetterOriginalBehavior" and get reliable behavior in every release of MacOS X?

Thanks,
--
Joao Pavao

Related mailsAuthorDate
mlCalculate height of text in a text field pavao Nov 22, 23:12
mlRe: Calculate height of text in a text field João Pavão Nov 23, 13:36