Smoothing looks different than system ones when using NSString drawAtPoint:

I have a custom state element view where I draw a string using NSString drawAtPoint:withAttributes: Compared to a system clock with the same text, it seems that my text lacks smoothing of the sub-pixel and looks like grainy. I found a recommendation to move the drawing point from (x, y) to (x + 0.5, y + 0.5), but that didn't help. The default view and setTitle: produce the same result.

What it looks like:

enter image description here

It seems that the system clock is a little light gray below, but I could not imitate it by drawing a line a second time in light gray.

+4
source share
3 answers

I do not see "font smoothing" in the rendering of the system or in any menu names on my machine. If it was turned on, you will see red and blue tinted pixels at the edges of the characters, not just gray ones. The difference is obvious with an increase.

You might want to experiment with turning subpixel positioning and quantization on or off using CGContextSetShouldSubpixelPositionFonts , CGContextSetShouldSubpixelQuantizeFonts , etc.

Otherwise, the main difference is that there is a faint white shadow in the rendering of the system. Try setting the context shadow to the offset {0,1} (or maybe {0, -1} if your context is upside down?), A blur of 0 or 1 and a color of 100% white at 30% alpha - which looks pretty close to to me.

+1
source

I assume that you are using

  UIGraphicsBeginImageContext(<#CGSize size#>); 

to initialize the context. On retina displays, this results in blurry font images. Use

  UIGraphicsBeginImageContextWithOptions(myFrameSize, NO, 0.0f); 

to fix it.

Also, it seems that if you are using Layer, define yourself as a delegate of the layer and make your drawing:

 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { NSGraphicsContext *nsGraphicsContext; nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:nsGraphicsContext]; // drawing here... [NSGraphicsContext restoreGraphicsState]; } 

It will also work.

0
source

Try:

 CGContextRef ctx = [NSGraphicsContext currentContext].graphicsPort; CGContextSetShouldSmoothFonts(ctx, true); 

I also searched for this for a long time. Got this from a WWDC video (I think it was "Best Practice for Cocoa Animation")

0
source

All Articles