views:

538

answers:

1

I am trying to create an app that scales up nicely on the iPhone 4. Currently most of it scales up perfectly, except for one crucial piece: the text that I draw inside a CALayer, inside its drawInContext: method. Here is my code:

- (void)drawInContext:(CGContextRef)context {
    UIGraphicsPushContext(context);

    CGContextSetGrayFillColor(context, 1.0f, 1.0f);
    CGContextFillRect(context, self.bounds);

    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);

    CGContextSetAllowsFontSmoothing(context, true);
    CGContextSetShouldSmoothFonts(context, true);

    CGContextSetAllowsFontSubpixelQuantization(context, true);
    CGContextSetShouldSubpixelQuantizeFonts(context, true);

    CGContextTranslateCTM(context, 0.0f, self.frame.size.height);
    CGContextScaleCTM(context, 1.0f, -1.0f);

    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSelectFont(context, "CardKit", 30.0f, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowText(context, "A", sizeof("A"));

    UIGraphicsPopContext();
}

This short produce crisp text on both devices, but unfortunately, it produces blurry text on both. Here is how it appears:

ugly text

That image is taken at 100% zoom on the iPhone 4. What in the world? Any ideas how I can fix this?

+2  A: 

Since you're dealing with CALayers, you need to set the contentsScale property of your layer to 2.0.

See this section in the iPhone Application programming guide for iOS 4.

Jason Foreman
Worked perfectly! Thanks a bunch!
Carter Allen
what happens for non retina display ? Will the text be 50% smaller ?
thierryb