问题描述
我正在尝试创建一个可以在iPhone 4上很好地扩展的应用程序。目前大多数都可以完美地扩展,除了一个关键部分:我在一个CALayer中绘制的文本,在其drawInContext:方法中。这是我的代码:
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:
该图像是在iPhone上以100%变焦拍摄的4.世界上有什么?我有什么想法可以解决这个问题吗?
That image is taken at 100% zoom on the iPhone 4. What in the world? Any ideas how I can fix this?
推荐答案
你应该设置图层的 contentsScale
与屏幕的比例
相同:
You should set the layer's contentsScale
to be the same as the scale
of your screen:
layer.contentsScale = [UIScreen mainScreen].scale;
这将在所有iOS设备,Retina Display和非Retina Display上正确缩放。除非你有充分的理由这样做,否则你不应该硬编码为2.0(或其他任何事情)。
This will scale it correctly on all iOS devices, Retina Display and non-Retina Display. You shouldn't hard-code it to 2.0 (or anything else for that matter) unless you have a good reason to do so.
这篇关于CGContext文本绘图无法在iPhone 4上扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!