Re: Calculate height of text in a text field
Re: Calculate height of text in a text field
- Subject: Re: Calculate height of text in a text field
- From: João Pavão <email@hidden>
- Date: Tue, 23 Nov 2004 12:36:27 +0000
I'll reply to myself...
I had tried the solution described in my previous message (assembling
the text system by hand) because it was the suggestion of another
thread in this mailing list's archives. However, while investigating
something else I came across -[NSCell cellSizeForBounds:], which when
given the text field's bounds as argument, apparently returns a NSSize
with the width and height needed to display the whole text contained in
the text field!! Well, it works very well, and since who's calculating
the height is the same object that draws the text (the
NSTextFieldCell), I guess it will always give me the correct value. :-)
--
Joao Pavao
On Nov 22, 2004, at 22:12, pavao wrote:
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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden