Re: Drawing text chunks (e.g. paras) in a text view with alternating background colours
Re: Drawing text chunks (e.g. paras) in a text view with alternating background colours
- Subject: Re: Drawing text chunks (e.g. paras) in a text view with alternating background colours
- From: Ross Carter <email@hidden>
- Date: Thu, 24 Sep 2009 13:20:32 -0400
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 (email@hidden)
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:
This email sent to email@hidden