Hi Ross,

Many thanks for the reply.

The reason I have to use a larger rect than just what gets passed in is that I 
had so many drawing problems and artefacts if I didn't expand it to the whole 
visible rect, for some reason.

> 4. Cache some of this information instead of recalculating
> it. For example, if you have one text view in a scroll view,
> all you really need to know is the origin.y of the first
> glyph of each text storage. Because the view background
> color is always a rectangle as wide as the text view, if you
> have an array of all the origin.y values of the text
> storages, you can take any rect and calculate the rects that
> need to be drawn in the respective colors.

The trouble with this is that it still has to calculate the Y position each 
time a letter is typed, because there's no way of knowing if the letter typed 
has moved all of the text beneath down (or up in the case of a deletion) or not.

> 2. Rather than calculate the range of characters from a
> drawing rect, use the information provided in
> NSLayoutManager -drawBackgroundForGlyphRange:atPoint:.
> 

You know, I think this might be a good solution. I think I may need to get rid 
of the alternating colours altogether and draw some sort of separator between 
the different text storages using this NSLayoutManager method. Either way, I'm 
hopeful that this will be a better solution than trying to get the character 
range in the text view's drawing methods.

Many thanks again!
All the best,
Keith

--- On Thu, 9/24/09, Ross Carter <rosscarter...@me.com> wrote:

> From: Ross Carter <rosscarter...@me.com>
> Subject: Re: Drawing text chunks (e.g. paras) in a text view with alternating 
> background colours
> To: "Keith Blount" <keithblo...@yahoo.com>, "Cocoa-dev" 
> <cocoa-dev@lists.apple.com>
> Date: Thursday, September 24, 2009, 5:20 PM
> Hi, Keith. Couple of thoughts:
> 
> > In my NSTextView subclass’s
> -drawViewBackgroundInRect: method, I get the range of
> characters in the current -visibleRect: using code similar
> to this:
> 
> -visibleRect is a much larger rect than you need, I should
> think. What's wrong with using the rect passed to
> -drawViewBackgroundInRect:?
> 
> 
> > To draw the background colours, ensuring that they fit
> perfectly behind my alternating ranges of text, I use
> NSLayoutManager’s -rectArrayForCharacterRange:... on the
> component ranges that are visible, then join the resultant
> rects (and expand to allow for the text container inset if
> necessary) and fill them with colour.
> 
> Aren't you drawing rects that do not need to be redrawn? Or
> maybe you are using needsToDrawRect:?
> 
> 
> > So my question is, can anyone think of a better, more
> efficient way of solving this problem? Perhaps I am
> overcomplicating things. In case I haven’t explained
> clearly enough, what I am trying to do is very similar to,
> say, drawing alternating paragraphs in a text view with a
> different background colour (and again, I don’t mean as in
> NSBackgroundColorAttributeName, but a rectangle of
> background colour going from the left of the view to the
> right). Only in my case, each component may contain multiple
> paragraphs, of course.
> 
> These things come to mind. They might or might not work:
> 
> 1. I don't know whether the NSBackgroundColorAttributeName
> attribute is (or could be) the same color as the one you are
> drawing for the view background. If it is, you could let the
> text system handle the bg color for the text areas, and just
> worry about the rects that are not getting painted by the
> text system.
> 
> 2. Rather than calculate the range of characters from a
> drawing rect, use the information provided in
> NSLayoutManager -drawBackgroundForGlyphRange:atPoint:.
> 
> 3. Use QuartzDebug with "Flash Identical Screen Updates" to
> look for areas that are getting redrawn needlessly.
> 
> 4. Cache some of this information instead of recalculating
> it. For example, if you have one text view in a scroll view,
> all you really need to know is the origin.y of the first
> glyph of each text storage. Because the view background
> color is always a rectangle as wide as the text view, if you
> have an array of all the origin.y values of the text
> storages, you can take any rect and calculate the rects that
> need to be drawn in the respective colors.
> 
> Hope this helps!
> 
> Ross



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to