Re: Text Sizing
Re: Text Sizing
- Subject: Re: Text Sizing
- From: "Clark S. Cox III" <email@hidden>
- Date: Wed, 1 Aug 2001 21:06:53 -0400
On Wednesday, August 1, 2001, at 11:28 , Douglas Davidson wrote:
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.
Where might I find this aforementioned sample code? I've been
searching to no avail. What I am doing seems quite simple, so I'm
abviously missing something. I have an NSAttributedString. I am creating
a new view of my own class, and printing it. I want to be able to resize
this view to be able to hold a single image, and my string before
printing, so that the pagination is correct.
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.