当在CGContext中绘制NSAttributedString时,看起来很丑的文本 [英] Ugly looking text when drawing NSAttributedString in CGContext

查看:507
本文介绍了当在CGContext中绘制NSAttributedString时,看起来很丑的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CoreAnimation图层中显示字符串,但不幸的是,CATextLayer还不够,主要是因为在使用约束时很难使用



我使用NSLayoutManager,使用下面的代码(PyObjC):

  NSGraphicsContext.saveGraphicsState 

#解决这个问题
CGContextSetShouldSmoothFonts(ctx,False)

graphics = NSGraphicsContext.graphicsContextWithGraphicsPort_flipped_(ctx,True)
NSGraphicsContext.setCurrentContext_ )

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()

,但唯一的问题是它产生了不好看的文本(虽然是反对的)。



这里是CATextLayer版本:



这里是NSLayoutManager版本:



我遗漏了什么?

解决方案>

我回答这个问题,因为coretext-dev档案不可搜索,来自苹果的Aki Inoue刚刚回答了我的问题:


由于CALayer不能表示子像素颜色(又名字体平滑),您需要禁用它。
我相信CATextLayer是默认做的。



做CGContextSetShouldSmoothFonts(上下文,假)。


感谢Aki!



Milen Dzhumerov的另一个评论:


我不相信这是准确的。我们使用子像素消除锯齿将文本绘制到CALayers中。你只需要确保你在绘制文本之前已经绘制了文本。请参见 http://www.cocoabuilder.com/archive/message / cocoa / 2008/3/28/202581


Milen是正确的,颜色,您可以:

  CGContextSetRGBFillColor(ctx,r,g,b,a)
CGContextFillRect ,(topLeft,size))
CGContextSetShouldSmoothFonts(ctx,True)

子像素抗锯齿文字。但是,如果您不知道背景颜色,则需要关闭字体平滑功能,否则会出现乱码结果。


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?

解决方案

I'm answering this because the coretext-dev archives are not searchable, and Aki Inoue from Apple just answered my question:

Since CALayer cannot represent subpixel color (aka font smoothing), you need to disable it. I believe CATextLayer does it by default.

Do CGContextSetShouldSmoothFonts(context, false).

Thanks, Aki!

Another comment by Milen Dzhumerov:

I don't believe this is accurate. We're drawing text into CALayers with subpixel anti-aliasing. You just have to make sure that you've drawn behind the text before drawing the text itself. See http://www.cocoabuilder.com/archive/message/cocoa/2008/3/28/202581 for references.

Milen is correct, in case you know the background colour beforehand, you can do:

CGContextSetRGBFillColor(ctx, r, g, b, a)
CGContextFillRect(ctx, (topLeft, size))
CGContextSetShouldSmoothFonts(ctx, True)

And you get pretty sub-pixel anti-aliased text. However, if you don't know the background colour, you need to turn off font smoothing or you'll get garbled results.

这篇关于当在CGContext中绘制NSAttributedString时,看起来很丑的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆