我想在 CoreAnimation 图层中显示字符串,但不幸的是 CATextLayer 是不够的,主要是因为在使用约束时很难使用,并且要包装文本。
我正在使用 NSLayoutManager,使用以下代码(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()
这一切都很好并且可以工作,但唯一的问题是它会产生难看的文本(尽管它是反锯齿的)。
有什么我想念的吗?