I have a NSView
elements that are being reused primarily to draw different text as they scrolls within an NSScrollView
. I am using the trio of NSTextStorage, NSLayoutManager and NSTextContainer
to display the text and the following is the call made to draw the text within the drawRect method:
layoutManager.drawGlyphsForGlyphRange(glyphRange, atPoint: NSMakePoint(0, 0))
What I am seeing is that on successive calls to drawRect
it overlays the new text to be drawn on top of the old text effectively creating a big mess/pile of text visually.
What is the best way of clearing the text drawn on the last call to drawRect, specifically with scrolling performance in mind:
A) Calling NSRectFill
in drawRect
prior to drawing the text - this effectively paints over the previous text and is pretty simple. It just seems wasteful to keep painting the same background over-and-over on every scroll.
B) Look into NSView.prepareForReuse()
- this approach would seem better suited to a scenario where you want to "wipe" the whole content of the existing object to reuse and start from a different state. In my scenario, it is just the previous text written from NSLayoutManager
that I want to get rid of.
C) Some other approach?