Re: Text Sizing
Re: Text Sizing
- Subject: Re: Text Sizing
- From: Douglas Davidson <email@hidden>
- Date: Wed, 1 Aug 2001 08:28:54 -0700
On Tuesday, July 31, 2001, at 03:18 PM, Clark S. Cox III wrote:
I have not yet been able to find any documentation on how one
would find out how much vertical space I would need to display a given
NSAttributedString in a given width.
That is, I have a NSAttributedString object that I am drawing with
-drawInRect:. I know what width I want the rectangle to be, but I have
no idea how high it should be, as the string is generated at run-time.
There are several possible approaches here. One would be to use the
text system directly rather than indirectly through the NSStringDrawing
convenience methods. This would involve creating an
NSTextStorage/NSLayoutManager/NSTextContainer set, hooking them up, and
setting the contents of the text storage to your attributed string.
Then the layout manager will tell you anything you could want to know
about size etc., and will do the drawing for you as well. This is
substantially more code than a simple drawInRect:, although we have some
sample code (and more on the way). It involves somewhat more memory
overhead for the new objects, but it can also be considerably more
efficient if the string needs to be drawn repeatedly.
Another option would be to use an NSCell, in particular a text cell.
Cells know how to draw and size themselves, among their many other
capabilities. There is nothing that says that cells have to be used
only in the standard pairs like NSButton/NSButtonCell,
NSTextField/NSTextFieldCell, etc.; you can also use them for your own
purposes.
I'm sure there are other options, and perhaps other people will propose
some. The best solution depends a bit on your constraints and on the
pattern of your drawing--how many different strings, how often, and so
on.
Douglas Davidson