Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
- Subject: Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
- From: Stéphane Sudre <email@hidden>
- Date: Wed, 9 Jul 2008 00:44:45 +0200
On Jul 8, 2008, at 1:26 AM, Jean-Daniel Dupas wrote:
Le 7 juil. 08 à 23:54, Stéphane Sudre a écrit :
On Jul 6, 2008, at 11:26 PM, Jean-Daniel Dupas wrote:
Le 6 juil. 08 à 21:25, Stéphane Sudre a écrit :
Problem:
--------
I would like/need to know the height that would be required to
render a string inside a fixed width box.
Solution that does not work:
----------------------------
So far, I've been using a solution found from a google search
and which looks like this:
- (float) heightOfString:(NSString *) inString forFont:(NSFont
*) inFont andMaxWidth:(float) inMaxWidth
{
float tHeight=0;
if (inString!=nil)
{
[...]
}
return tHeight;
}
usedRectForTextContainer: causes neither glyph generation nor
layout, and so the result is may be inaccurate (the layout
manager may be computing the layout in background and does not
have finish).
You have to make sure it is ready before using this function.
[tLayoutManager ensureLayoutForTextContainer:tTextContainer];
I'm running on Mac OS X 10.4.11, this method is only available on
Leopard and later.
But this made me think of trying setBackgroundLayoutEnabled: with
NO. Yet it does not solve the issue.
Maybe someone has a better solution, but I think you can mimic the -
ensureLayoutForTextContainer: method behavior using this:
[layoutManager boundingRectForGlyphRange:[layoutManager
glyphRangeForTextContainer:myContainer] inTextContainer:myContainer];
The doc says this method "Performs glyph generation and layout if
needed." And after you can send an -usedRectForTextContainer: message.
Same results. Same incorrect values.
What I mean by incorrect value is that if I draw the string inside a
rectangle whose width is inMaxWidth and height is the value obtained
from the rect returned by usedRectForTextContainer:, the last line of
the string is clipped outside the rectangle. And the difference
increases with the number of lines.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden