1

I am using the trio of NSLayoutManager, NSTextStorage & NSTextContainer to render text in a layer-backed NSView.

I am under the impression that the view will be more performant if I can override wantsUpdateLayer to return true and thus be using layers more fully. Is this true?

However, all examples I have seen of using NSLayoutManager say that you need to place the following call within drawRect:

layoutManager.drawGlyphsForGlyphRange(glyphRange, atPoint: NSMakePoint(0, 0)) 

How would you perform the equivalent call within updateLayer or some other more layer-oriented place instead?

Sam
  • 2,745
  • 3
  • 20
  • 42

1 Answers1

0

I created a custom CALayer and overrode the drawInContext method as below:

override func drawInContext(ctx: CGContext!) {
    NSGraphicsContext.saveGraphicsState()

    var nsgc = NSGraphicsContext(CGContext: ctx, flipped: false)
    NSGraphicsContext.setCurrentContext(nsgc)

    NSColor.whiteColor().setFill()
    NSRectFill(NSMakeRect(0, 0, 84, 24))

    lm.drawGlyphsForGlyphRange(glyphRange, atPoint: NSMakePoint(0, 0))  // location of textContainer

    NSGraphicsContext.restoreGraphicsState()

}

This gets the job done, but

  1. I'm not sure if there are any performance implications to saving and restoring the graphics context as above.
  2. I am unsure how the same would be achieved in the NSView's updateLayer method as that does not have a context to use.
Sam
  • 2,745
  • 3
  • 20
  • 42
  • in UIKit, a view is just a wrapper around a CALayer that handles the user interaction. On OS X I am using a custom view in a similar way to draw text. So drawing in CALayer is not an issue. – user965972 Oct 15 '15 at 11:01
  • Saving and restoring graphicscontext once or twice every drawInContext call should not be an issue. Don't call it hundreds of times within the same function though. And you can always profile your code if it appears slow. – user965972 Oct 15 '15 at 11:03