【发布时间】:2011-04-18 10:39:27
【问题描述】:
我创建了一个CALayer 并添加了CATextLayer,文本变得模糊。在文档中,他们谈论“亚像素抗锯齿”,但这对我来说意义不大。任何人都有一个代码 sn-p 可以生成 CATextLayer 并带有一些清晰的文本?
以下是 Apple 文档中的文字:
注意:CATextLayer 在渲染文本时禁用亚像素抗锯齿。 文本只能使用亚像素抗锯齿绘制 同时合成到现有的不透明背景中 它被光栅化了。无法通过以下方式绘制子像素抗锯齿文本 本身,无论是成图像还是图层,分别提前 有背景像素来编织文本像素。环境 图层的 opacity 属性为 YES 不会改变渲染 模式。
第二句话暗示如果将composites 转换为existing opaque background at the same time that it's rasterized. 可以得到好看的文本,这很好,但是我如何合成它以及如何给它一个不透明的背景以及如何光栅化它?
他们在 Kiosk 菜单示例中使用的代码如下:(它是 OS X,不是 iOS,但我认为它可以工作!)
NSInteger i;
for (i=0;i<[names count];i++) {
CATextLayer *menuItemLayer=[CATextLayer layer];
menuItemLayer.string=[self.names objectAtIndex:i];
menuItemLayer.font=@"Lucida-Grande";
menuItemLayer.fontSize=fontSize;
menuItemLayer.foregroundColor=whiteColor;
[menuItemLayer addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMaxY
relativeTo:@"superlayer"
attribute:kCAConstraintMaxY
offset:-(i*height+spacing+initialOffset)]];
[menuItemLayer addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMidX
relativeTo:@"superlayer"
attribute:kCAConstraintMidX]];
[self.menuLayer addSublayer:menuItemLayer];
} // end of for loop
谢谢!
编辑:添加我实际使用的导致文本模糊的代码。这是来自我发布的一个相关问题,即添加 UILabel 而不是 CATextLayer 而是得到一个黑匣子。 http://stackoverflow.com/questions/3818676/adding-a-uilabels-layer-to-a-calayer-and-it-just-shows-black-box
CATextLayer* upperOperator = [[CATextLayer alloc] init];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat components1[4] = {1.0, 1.0, 1.0, 1.0};
CGColorRef almostWhite = CGColorCreate(space,components1);
CGFloat components2[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef almostBlack = CGColorCreate(space,components2);
CGColorSpaceRelease(space);
upperOperator.string = [NSString stringWithFormat:@"13"];
upperOperator.bounds = CGRectMake(0, 0, 100, 50);
upperOperator.foregroundColor = almostBlack;
upperOperator.backgroundColor = almostWhite;
upperOperator.position = CGPointMake(50.0, 25.0);
upperOperator.font = @"Helvetica-Bold";
upperOperator.fontSize = 48.0f;
upperOperator.borderColor = [UIColor redColor].CGColor;
upperOperator.borderWidth = 1;
upperOperator.alignmentMode = kCAAlignmentCenter;
[card addSublayer:upperOperator];
[upperOperator release];
CGColorRelease(almostWhite);
CGColorRelease(almostBlack);
编辑 2:请参阅下面的答案以了解如何解决。 sbg.
【问题讨论】:
-
这不会回答您的问题,但可能与像我这样只想绘制属性文本的人相关,其方式与使用 UILabel 最相似:stackoverflow.com/questions/3786528/…
标签: text ios core-animation calayer