如何清除CATextLayer中的文本

Ste*_*eve 91 text core-animation calayer ios

我做了CALayer一个添加CATextLayer,文字出来模糊.在文档中,他们谈论"亚像素抗锯齿",但这对我来说并不重要.任何人都有一个代码片段,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 
Run Code Online (Sandbox Code Playgroud)

谢谢!


编辑:添加我实际使用的代码导致文本模糊.这是来自我发布的一个相关问题,即添加一个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);
Run Code Online (Sandbox Code Playgroud)

编辑2:请参阅下面的答案,了解这是如何解决的.SBG.

Ste*_*eve 223

简答 - 您需要设置内容缩放:

textLayer.contentsScale = [[UIScreen mainScreen] scale];
Run Code Online (Sandbox Code Playgroud)

前一段时间我了解到,当您拥有自定义绘图代码时,您必须检查视网膜显示并相应地缩放图形. UIKit负责大部分工作,包括字体缩放.

不是这样的 CATextLayer.

我的模糊来自于一个.zPosition不是零,也就是说,我有一个应用于我父层的变换.通过将其设置为零,模糊性消失了,并被严重的像素化所取代.

在搜索高低之后,我发现你可以设置.contentsScale为a CATextLayer,你可以将其设置[[UIScreen mainScreen] scale]为匹配屏幕分辨率.(我认为这适用于非视网膜,但我没有检查 - 太累了)

包括这个后,我CATextLayer的文字变得清脆.注意 - 父层不需要.

而且模糊不清?当你在3D中旋转时它会回来,但你没有注意到它,因为文本开始时是清晰的,而它在运动时,你无法分辨.

问题解决了!

  • 这是简短的回答:`textLayer.contentScale = [[UIScreen mainScreen] scale];`顺便说一句,我也喜欢很长的答案:) (37认同)
  • 更正`_textLayer.contentsScale = [[UIScreen mainScreen] scale];`你错过了一个"s";) (17认同)

Sur*_*gch 33

迅速

将文本图层设置为使用与屏幕相同的比例.

textLayer.contentsScale = UIScreen.main.scale
Run Code Online (Sandbox Code Playgroud)

之前:

在此输入图像描述

后:

在此输入图像描述


Mat*_*ong 17

首先,我想指出你已经用iOS标记了你的问题,但约束管理器只能在OSX上使用,所以我不确定你是如何让它工作的,除非你能够链接它不知何故在模拟器中.在设备上,此功能不可用.

接下来,我将指出我经常创建CATextLayers并且从未遇到过您所指的模糊问题,所以我知道它可以完成.简而言之,这种模糊的发生是因为您没有将图层放在整个像素上.请记住,在设置图层的位置时,可以使用x和y的浮点值.如果这些值在小数点后面有数字,则图层将不会定位在整个像素上,因此会产生这种模糊效果 - 其程度取决于实际值.要测试这一点,只需创建CATextLayer并将其显式添加到图层树,确保您的位置参数位于整个像素上.例如:

CATextLayer *textLayer = [CATextLayer layer];
[textLayer setBounds:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)];
[textLayer setPosition:CGPointMake(200.0f, 100.0f)];
[textLayer setString:@"Hello World!"];

[[self menuLayer] addSublayer:textLayer];
Run Code Online (Sandbox Code Playgroud)

如果您的文字仍然模糊,那么还有其他问题.文本图层上的模糊文本是错误编写代码的工件,而不是图层的预期功能.将图层添加到父图层时,您可以将x和y值强制转换为最接近的整个像素,它应该可以解决模糊问题.


Gab*_*abe 14

在设置shouldRasterize之前,您应该:

  1. 设置要栅格化的基础图层的rasterizationScale
  2. 设置任何CATextLayers和其他类型的图层的contentsScale属性(它永远不会伤害它)

如果你不做#1,那么子层的视网膜版本看起来会很模糊,即使对于普通的CALayers也是如此.

- (void)viewDidLoad {
  [super viewDidLoad];

  CALayer *mainLayer = [[self view] layer];
  [mainLayer setRasterizationScale:[[UIScreen mainScreen] scale]];

  CATextLayer *messageLayer = [CATextLayer layer];
  [messageLayer setForegroundColor:[[UIColor blackColor] CGColor]];
  [messageLayer setContentsScale:[[UIScreen mainScreen] scale]];
  [messageLayer setFrame:CGRectMake(50, 170, 250, 50)];
  [messageLayer setString:(id)@"asdfasd"];

  [mainLayer addSublayer:messageLayer];

  [mainLayer setShouldRasterize:YES];
}
Run Code Online (Sandbox Code Playgroud)