Re: Customized NSTextView drawing
Re: Customized NSTextView drawing
- Subject: Re: Customized NSTextView drawing
- From: Mark Alldritt <email@hidden>
- Date: Tue, 11 May 2004 09:22:27 -0700
>
> I have some Carbon/WASTE code that does syntax highlighting that I
>
> want to
>
> port to Cocoa. The approach I use is to hook into WASTE's drawing
>
> code to
>
> syntax color "on-the-fly". This avoids the overheads and delays of
>
> having
>
> to style all the text as the user types.
>
>
>
> How would one go about this with Cocoa's NSTextView/NSTextStorage
>
> combination? I've poked around in the documentation, but I cannot
>
> find a
>
> method that I need to override that delivers styled text runs to
>
> NSTextView
>
> for rendering. Where should I be looking?
>
>
The Cocoa text system offers several mechanisms that you could use.
>
First, of course, you could alter the colors in the NSTextStorage.
>
That would change the actual contents of the document, and would give
>
you colors that would be persistent during saving or copy/pasting as
>
rich text. It would also incur extra processing costs, as you
>
describe.
>
>
A second alternative would be to use NSLayoutManager's temporary
>
attributes. These are non-persistent, drawing-only attributes that the
>
layout manager stores, manages, and displays for you. That is probably
>
the simplest way of doing what you suggest. Look in the
>
NSLayoutManager documentation for more information.
>
>
Finally, you can subclass NSLayoutManager and override certain of the
>
drawing methods, since it is NSLayoutManager that actually prepares the
>
glyphs for rendering by Quartz. The basic drawing methods are
>
-drawBackgroundForGlyphRange:atPoint: and
>
-drawGlyphsForGlyphRange:atPoint:. The latter divides the glyphs up
>
into runs and then calls
>
showPackedGlyphs:length:glyphRange:atPoint:font:color:
>
printingAdjustment:. If you want to do extra drawing behind or before
>
the glyphs, then you would probably override one of the former two
>
methods. If you merely want to change the color used for the glyphs,
>
then you would probably override the latter. However, temporary
>
attributes are probably easier if that is all you need to do.
Thanks for the summary of my options. I've experimented with temporary
attributes and they will do much of what I want to do.
I would like to experiment with altering attributes other than color and
underlining. To do this, I think I need to override NSTextStorage's
attributesAtIndex:effectiveRange:. I presume there is some notification I
need to generate letting the layout manager that it must re-layout a range
of text. Are there any good code examples showing how to subclass
NSTextStorage? My attempts so far have come to nothing.
Cheers
-Mark
---------------------------------------------------------------------
Mark Alldritt Late Night Software Ltd.
Phone: 250-380-1725 333 Moss Street
FAX: 250-383-3204 Victoria, B.C.
WEB:
http://www.latenightsw.com/ CANADA V8V-4M9
AIM: markalldritt
_______________________________________________
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.