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.
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.
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
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
.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