Re: Bounding rect for String when wrapped
Re: Bounding rect for String when wrapped
- Subject: Re: Bounding rect for String when wrapped
- From: Jonathan Jackel <email@hidden>
- Date: Mon, 16 Sep 2002 09:47:56 -0400
Ok, I was a bit of an idiot when I answered here because I didn't really
understand the question. Now I am in the same fix and I wonder, Matt, if
you could be a little more explicit about your tip to use sizeToFit.
Also, in checking the archives I notice that Ben Haller had a similar
problem back in February. Did you solve it, Ben?
Here is what I am trying to do. I am printing a document with two columns
per page. I have an array of attributed strings, each string built
line-by-line until its size.height exceeds the height of a column (roughly,
half a page folded vertically) of text. Some of the lines are a little
wider than a column, though.
Then I draw each string in a view so that I can get two columns on a page
for printing. But when I draw a string in a rect the size of a column, it
is chopped off at the bottom, apparently because the wider lines wrap and
take up extra vertical space that is not measured by [atString size]. If
[atString size] knew the width of the column, it might be able to return the
proper height, but it apparently assumes the string will be drawn in an
infinitely wide rect.
There are two problems with using sizeToFit, it seems. First, NSView does
not respond to it. Would you use NSBox instead? Or what?
Second, the view resizes itself to fit the text. Does it resize itself
horizontally as well as vertically? If so, the resulting height will be
fairly close to what I get with [atString size], so I would still get the
wrong answer.
The solution I am now contemplating is to check the width of each line of
the string as it is being built, and inserting hard returns in lines that
are too wide. This should allow [atString size] to give me an accurate
height. If anyone has a better idea, I am all ears.
Jonathan
on 9/11/02 1:20 PM, Jonathan E. Jackel at email@hidden wrote:
>
> On Wed, 11 Sep 2002 01:36:53 EDT, email@hidden said:
>
>
>
>> I can use drawInRect:withAtrributes: to draw the String in a
>
> given rectangle
>
>> with word wrapping, but first I need to calculate the rectangle.
>
> I know the
>
>> width, but am having problems calculating the height.
>
>
>
> What I do is call -sizeToFit and then look to see what the height is.
>
> Perhaps this is somewhat crude and not the right way, but it
>
> works great. m.
>
>
>
What about -(NSSize)sizeWithAttributes: in NSString Additions,
>
or -(NSSize)size in NSAttributedString Additions?
>
>
Jonathan
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.