我想显示内部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()
Run Code Online (Sandbox Code Playgroud)
这一切都很好并且有效,但唯一的问题是它会产生看起来很糟糕的文本(虽然它是防腐蚀的).
这是CATextLayer版本:http://i39.tinypic.com/23h0h1d.png
这是NSLayoutManager版本:http://i40.tinypic.com/2vv9rw5.png
我缺少什么?