Re: string drawing again
Re: string drawing again
- Subject: Re: string drawing again
- From: Alastair Houghton <email@hidden>
- Date: Thu, 5 Feb 2004 19:36:36 +0000
On 4 Feb 2004, at 23:30, Chris Meyer wrote:
>
I'm trying to draw a string in a sub-rectangle of a non-flipped
>
graphics context. The string needs to be justified to the sides of the
>
sub-rectangle.
>
>
I can't use the NSString methods drawInRect or drawAtPoint because
>
they clip the right side of large italicized strings.
>
>
I can't use the text layout classes because they (strangely) require a
>
flipped coordinate system.
>
>
Any suggestions?
You can use a subview to draw the text; you only need a single subview,
even if you're drawing many strings. A quick breakdown of what you
need to do:
1. Create or otherwise obtain a view that can render the text you need.
You could use an NSTextView, you could use the window's field editor
(which is an NSTextView, although the -fieldEditor:forObject: method
returns an NSText, NSTextView's superclass), or you could use your own
class that uses NSString or NSAttributedString's methods, or even
ATSUI.
2. In your -drawRect:, add the view as a subview of the view you are
drawing.
3. For each bit of text:
(i) Move the subview to the appropriate place in your view.
(ii) Configure the subview to render it.
(iii) Send the subview a -display message to make it do the drawing.
4. Remove the subview again.
This type of approach has the advantage that your subview can be
flipped even though the view it is in is not. When I started using
Cocoa, I kept thinking of the views like windows in Win32 (so moving
them around or altering the hierarchy would cause a redraw), whereas
they are actually much lighter-weight than that... so it is perfectly
reasonable to add and remove them like this.
BTW, it isn't strange that the text system requires a flipped
co-ordinate system. If the co-ordinates weren't flipped, then the text
system wouldn't be able to render the first line of text until it had
completed layout for *all* of the text in the view, because there would
be no way to know how tall the text was.
Kind regards,
Alastair.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.