9

我正在尝试创建一个可以在 iPhone 4 上很好地扩展的应用程序。目前,除了一个关键部分:我在 CALayer 中绘制的文本,在它的 drawInContext: 方法中,它的大部分都可以完美地扩展。这是我的代码:

- (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();
}

这段短片在两种设备上都产生了清晰的文本,但不幸的是,它在两个设备上都产生了模糊的文本。这是它的外观:

丑陋的文字 http://files.droplr.com.s3.amazonaws.com/files/16285043/1gBp61.Screen%20shot%202010-06-26%20at%2021:25:09.png

该图像是在 iPhone 4 上以 100% 缩放拍摄的。到底是什么?有什么想法可以解决这个问题吗?

4

3 回答 3

27

您应该将图层设置contentsScale为与scale屏幕相同:

layer.contentsScale = [UIScreen mainScreen].scale;

这将在所有 iOS 设备、Retina Display 和非 Retina Display 上正确缩放。除非您有充分的理由这样做,否则您不应将其硬编码为 2.0(或其他任何内容)。

于 2010-12-07T03:22:35.793 回答
2

由于您正在处理 CALayers,因此您需要将图层的 contentsScale 属性设置为 2.0。

请参阅适用于 iOS 4 的 iPhone 应用程序编程指南中的此部分

于 2010-07-13T17:03:32.740 回答
2

上一个答案的链接现在已断开。新链接就是这个

于 2010-11-25T07:27:38.477 回答