title: Faking Subpixel Antialiasing on Text with Core Animation
body: <p>For anyone working on a project with Core Animation layer-backed views, it's unfortunately obvious that subpixel antialiasing (text smoothing) is disabled for text not rendered on a pre-set opaque background. Now, for people who are able to set opaque backgrounds for their text (either with a <code>setBackgroundColor:</code> call or the equivalent setting in Interface Builder), this issue doesn't present too much of a problem. For others, though, who have absolutely no way to work around it, it's not something one can ignore.</p>
  
  <p>I've been looking online for well over two days for a solution, and nothing usable has come up. All I want to do is create a text label (not user-editable) that is set on a sheet window (sheet window backgrounds are transparent, so having the sheet's content view declare <code>wantsLayer</code> as true disables text smoothing for all labels on the window); something very simple. I've seen a lot of contested solutions (a quick Google search will bring up this topic in many other places), but so far, all of those solutions rely on people being able and willing to compromise with an opaque background (which you cannot use on a sheet).</p>
  
  <p>So far, the best direction I can imagine taking this in is pre-rendering the text with text smoothing onto an image, and then displaying the image as usual on a layer-backed view. However, this doesn't seem to be possible. The 'normal' way I would assume one would try is this:</p>
  
  <pre><code>NSAttributedString *string = [[self cell] attributedStringValue];
  NSImage *textImage = [[NSImage alloc] initWithSize:[string size]];
  [textImage lockFocus];
  [string drawAtPoint:NSZeroPoint];
  [textImage unlockFocus];
  </code></pre>
  
  <p>But that doesn't seem to work (most likely because when called from <code>drawRect:</code>, the graphics context set up already has text smoothing disabled - subpixel antialiasing is turned off, and regular antialiasing is used instead), but neither does the more involved solution found in <a href="https://stackoverflow.com/questions/4293487/how-to-work-around-poor-text-rendering-in-text-backed-by-a-calayer">this question</a> (where you create your own graphics context).</p>
  
  <p>So how is doing something like this possible? Can you somehow 'fake' the effect of text smoothing? Are there even hack-ish workarounds that will get something set up? I really don't want to have to abandon Core Animation just because of this silly issue; there are a lot of benefits to it that save a lot of time and code.</p>
  
  <hr>
  
  <p><strong>Edit:</strong> After a lot of searching, I found something. Although the <a href="http://www.cocoabuilder.com/archive/cocoa/228931-sub-pixel-font-smoothing-with-cgbitmapcontext.html" rel="nofollow noreferrer">thread itself</a> only reaffirms my suspicions, I think I may have found one solution to the problem: custom <code>CALayer</code> drawing. Timothy Wood, in one of his responses, attached a sample project that shows font smoothing using several techniques, several of which work. I'll look into integrating this into my project.</p>
  
  <p><strong>Edit #2:</strong> Turns out, the link above is a bust as well. Although the methods linked give subpixel antialiasing, they fail as well on transparent backgrounds.</p>