如何解决 CALayer 支持的文本中文本渲染不佳的问题
我在 NSTextField
中有一些可变文本,它们在 CALayer
背景视图上呈现。由于 CALayer 不支持对其上的任何文本进行子像素别名渲染,因此该文本看起来很垃圾。
一些谷歌搜索揭示了其原因,并且文本必须渲染到不透明背景上才能启用 SPA。在这种情况下,如果可能的话,我想避免渲染到不透明的背景上。有更好的解决方法吗?
我完全愿意将文本自己渲染成 NSImage ,如果这有帮助的话,但我找不到任何确认的报告。
它在 Interface Builder 中看起来绝对没问题,所以我知道秘密就在这台计算机内部的某个地方,只是想摆脱它。
I have some variable text in an NSTextField
that renders on a CALayer
background view. As a CALayer
does not support sub-pixel aliasing for text rendering of any text on top of it, this text looks rubbish.
A bit of googling reveals the reasons why this is, and that text must be rendered onto an opaque background to have SPA enabled. Rendering onto an opaque background is something I'd like to avoid if at all possible in this case. Is there a better workaround?
I am completely amenable to rendering the text myself into an NSImage
, if that will help, but I can't find any confirmed reports that it will.
It looks absolutely fine in Interface Builder so I know the secret is somewhere inside this computer just straining to get out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
找到解决方法。看起来,Quartz 中没有任何东西可以在透明背景上渲染具有子像素别名的文本。但是,您可以将文本渲染到屏幕外位图缓冲区,前提是已以正确的方式创建了屏幕外位图缓冲区。该缓冲区的背景必须是不透明的。
我的视图以前有一个稍微透明的 PNG 背景。我可以简单地将这个背景设置为不透明并毫无问题地渲染到它,但由于此视图需要淡入和淡出,因此它需要 CALayer 支持,因此文本会正确渲染一次,然后随后在没有子像素锯齿的情况下渲染。
这是我的代码。它看起来非常冗长,如果有人能帮助我简化它,我会很高兴。它假设您有一个名为
_title
的 NSImageView 和一个名为title
的 NSString。Workaround found. Nothing in Quartz can render text with Sub-Pixel Aliasing on top of a transparent background, seemingly. However, you CAN render text to an offscreen bitmap buffer, providing that offscreen bitmap buffer has been created in the correct fashion. The background of this buffer must be opaque.
My view previously had a PNG background that was slightly transparent. I could have simply made this background opaque and rendered to it without problem, but as this view needs to fade in and out, it needed to be CALayer-backed, so the text renders once properly, and then subsequently renders without Sub-Pixel Aliasing.
Here's my code. It seems incredibly verbose, I'd love it if anyone could help me whittle it down. It assumes you have an NSImageView called
_title
and an NSString calledtitle
.我不太确定你的限制是什么,或者为什么你绝对必须绘制到一个图层,但是 os4.1 除其他外,Core Text 已从桌面移植到 iOS。您可以利用它的高级类型设置功能沿着直线甚至弧线渲染字形,并让它为您完成大部分繁重的工作。它是一个相对较低级别的API,但速度非常快。
其他示例包括 CoreTextArcCocoa 和 CoreTextTest
I'm not quite sure what your limitations are, or why you absolutely have to draw to a layer, but new in os4.1 among other things, Core Text was ported to iOS from the desktop. You can probably take advantage of it's advanced type setting features to render glyphs along lines or even arcs and have it do most of the heavy lifting for you. It is a relatively low-level API, but it is very fast.
Other examples include CoreTextArcCocoa and CoreTextTest
这是原始海报不想这样做的方式,但对我来说,有一个不透明的背景是可以的......
当你制作图层时:
然后在
Here is how the Original poster did not want to do it, but it was OK for me to have an opaque background...
when you make the layer:
Then in
填充变量字符串后,文本的 X、Y 位置是多少?我的
UILabel
也遇到了类似的问题,这是由于文本的 XY 位置是浮点数造成的。 (它试图将变量数据居中,并且 XY 值是半像素)我截断了值并修复了模糊的文本。
What are the X, Y positions of the Text once it gets it's variable string filled in? I had a similar problem for a
UILabel
which was caused by the fact that the XY position of the text were floats. (it was trying to center variable data and the XY vals were half pixels)I truncated the values and it fixed the blurry text.