How to work around poor text rendering in text backed by a CALayer

前端 未结 4 1825
感动是毒
感动是毒 2021-01-31 23:16

I have some variable text in an NSTextField that renders on a CALayer background view. As a CALayer does not support sub-pixel aliasing fo

4条回答
  •  既然无缘
    2021-01-31 23:18

    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:

        layer.opaque = YES;
    

    Then in

      - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
    
         [NSGraphicsContext saveGraphicsState];
          NSGraphicsContext *nscg = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
          [NSGraphicsContext setCurrentContext:nscg];
    
          NSRect ourframe = self.frame;
          // white background enables antialiasing
          [[NSColor whiteColor] setFill];
          NSRect ourNSFrame = ourframe;
          ourNSFrame.origin = NSZeroPoint;
          NSRectFill(ourNSFrame);
    
         [sometext drawInRect:ourNSFrame withAttributes:someattrs];
    
         [NSGraphicsContext restoreGraphicsState];
    

提交回复
热议问题