Peterdk
Peterdk

Reputation: 16015

Font smoothing/anti-aliasing when using Quartz2d on iOS?

I have a custom UIView that generates some kind of diagram that needs a height notated with a number.

All drawing goes ok, it antialiases correctly when creating circles etc. with quartz2d.
However, when I want to draw a NSString to the context, the font-smoothing/anti-aliasing is messed up.
Disabling anti-aliasing does result in a thinner text, so the switch does work, however, the anti-aliased text is awfull. It doesn't look as crisp at all as the normal text rendered in iOS.

CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES); 
CGContextSetShouldSmoothFonts(context, YES);
NSString *stringValue = [NSString stringWithFormat:@"%d", input.height];
UIFont *font = [UIFont  systemFontOfSize:textsize]; 

[stringValue drawAtPoint:CGPointMake(x, y) withFont:font];

I am out of clues on how to solve this font rendering issue. Any ideas?

Upvotes: 1

Views: 3167

Answers (2)

Peterdk
Peterdk

Reputation: 16015

My solution to this problem was to just add several UILabel programmatically with the right coordinates. These UILabel instances rendered the text perfectly, so this solved my problem.

Upvotes: 2

broseborough
broseborough

Reputation: 31

If you are willing to forego the anti-aliasing on the text you could save the context with anti-aliasing turned on.

GCContextSaveGState(context);

Turn anti-aliasing off and render the text:

GCContextSetShouldAntialias(context, NO);
NSString *stringValue = [NSString stringWithFormat:@"%d", input.height];
UIFont *font = [UIFont systemFontOfSize:textSize];

[stringValue drawAtPoint:CGPointMake(x, y) withFont:font];

Then restore the graphics context back to it's previous space. (with anti-aliasing turned on) to render the remaining parts of the drawing.

CGContextRestoreGState(context);

Upvotes: 3

Related Questions