I want to display strings inside CoreAnimation layers, but unfortunately CATextLayer is not enough, mostly because it's difficult to use when using constraints and you want to wrap the text.
I am using NSLayoutManager, using the following code (PyObjC):
NSGraphicsContext.saveGraphicsState()
# THIS SOLVES THIS ISSUE
CGContextSetShouldSmoothFonts(ctx, False)
graphics = NSGraphicsContext.graphicsContextWithGraphicsPort_flipped_(ctx, True)
NSGraphicsContext.setCurrentContext_(graphics)
height = size.height
xform = NSAffineTransform.transform();
xform.translateXBy_yBy_(0.0, height)
xform.scaleXBy_yBy_(1.0, -1.0)
xform.concat()
self.textContainer.setContainerSize_(size)
glyphRange = self.layoutManager.glyphRangeForTextContainer_(self.textContainer)
self.layoutManager.drawBackgroundForGlyphRange_atPoint_(glyphRange, topLeft)
self.layoutManager.drawGlyphsForGlyphRange_atPoint_(glyphRange, topLeft)
NSGraphicsContext.restoreGraphicsState()
This is all fine and working, but the only issue is that it produces bad-looking text (although it is antialised).
Here's the CATextLayer version:
And here's the NSLayoutManager version:
Anything I'm missing?