在CALayer上的IoS 7模糊核心文本

时间:2014-11-26 01:29:12

标签: ios ios7

当Core Text呈现给CALayer文本模糊时,我遇到问题。我确实检查了位置是整体和框架的高度和宽度,但仍然文本模糊。 IB中的布局很简单我有标准的UIView,然后在代码中我将创建CALayer,将此图层附加到UIView图层并将文本绘制到此图层:

drawLayer:(CALayer *)图层inContext:(CGContextRef)context {

以下是代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view.layer setPosition:CGPointMake(lroundf(self.view.frame.size.width/2), lroundf(self.view.frame.size.height/2))];
    [self.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    CALayer *newLayer = [[CALayer alloc] initWithLayer:self.view.layer];
    newLayer.frame =CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    newLayer.delegate=self;
    [self.view.layer setPosition:CGPointMake(lroundf(self.view.frame.size.width/2), lroundf(self.view.frame.size.height/2))];
    [self.view.layer addSublayer:newLayer];
    [newLayer setNeedsDisplay];

}


-(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)currentContext{
   NSLog(@"drawLayer Layer position x %f pos y %f", layer.position.x,layer.position.y);
   NSLog(@"drawLayer Layer origin x %f y %f", layer.bounds.origin.x,layer.bounds.origin.y);
   NSLog(@"drawLayer Layer bounds width %f height %f", layer.bounds.size.width,layer.bounds.size.height);

    layer.contentsScale=[[UIScreen mainScreen]scale];
    CTFontRef fontRef =
    CTFontCreateWithName((CFStringRef)@"Didot", 36, NULL);
    NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)fontRef, (NSString *)kCTFontAttributeName, (id)[[UIColor blackColor] CGColor], (NSString *)(kCTForegroundColorAttributeName), nil];
    CFRelease(fontRef); 

    // Flip the coordinate system
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
    CGContextTranslateCTM(currentContext, 0, layer.bounds.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
    NSAttributedString *timeMarker = [[NSAttributedString alloc] initWithString:@"Hello blurry text !!!" attributes:attrDictionary] ;
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)timeMarker);
    CGContextSetTextPosition(currentContext, 0, 50);
    CTLineDraw(line, currentContext);
    CFRelease(line);
}

这是视图边界:

DrawRect[14767:15435976] View bounds <UIView: 0x7fe153d22980; frame = (0 0; 375 667); autoresize = W+H; autoresizesSubviews = NO; layer = <CALayer: 0x7fe153d21c40>>

Below is NSLOG:
2014-11-25 20:18:10.159 DrawRect[15103:15454664] viewDidLoad View Position x 187.500000 pos y 333.500000 
2014-11-25 20:18:10.159 DrawRect[15103:15454664] viewDidLoad View bounds width 375.000000 heights 667.000000
2014-11-25 20:18:10.159 DrawRect[15103:15454664] viewDidLoad View bounds origin x 0.000000 x 0.000000

2014-11-25 20:18:10.159 DrawRect[15103:15454664] viewDidLoad View frame size width 375.000000 height 667.000000
2014-11-25 20:18:10.159 DrawRect[15103:15454664] viewDidLoad View frame origin width 0.000000 height 0.000000

2014-11-25 20:18:10.160 DrawRect[15103:15454664] View Layer frame size width 375.000000 height 667.000000
2014-11-25 20:18:10.160 DrawRect[15103:15454664] View Layer origin width 0.000000 height 0.000000

2014-11-25 20:18:10.165 DrawRect[15103:15454664] drawLayer Layer position x 188.000000 pos y 334.000000
2014-11-25 20:18:10.165 DrawRect[15103:15454664] drawLayer Layer origin x 0.000000 y 0.000000
2014-11-25 20:18:10.165 DrawRect[15103:15454664] drawLayer Layer bounds width 375.000000 height 667.000000

2014-11-25 20:18:10.166 DrawRect[15103:15454664] drawLayer after CTM Layer position x 188.000000 pos y 334.000000
2014-11-25 20:18:10.166 DrawRect[15103:15454664] drawLayer after CTM Layer origin x 0.000000 y 0.000000
2014-11-25 20:18:10.174 DrawRect[15103:15454664] drawLayer after CTM Layer bounds width 375.000000 height 667.000000

2 个答案:

答案 0 :(得分:2)

线索是在调试器中打印CALayer对象(po 0x7fec22927c40)。

CABackingStore缓冲区与图层边界[375 853]具有相同的值,但根据比例定义,CAlayer应在缓冲区中绘制两次。

推断是第二次抽签被跳过了。我的layer.contentsScale在方法中:

drawLayer:(CALayer *)layer inContext:(CGContextRef)currentContext

当layer.contentsScale移动到viewDidLoad方法时,问题得到解决。

CALayer *newLayer = [[CALayer alloc] initWithLayer:self.view.layer];
    newLayer.frame =CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    newLayer.delegate=self;
    newLayer.contentsScale=2;

来自[375 853]的CABackingStore缓冲区变为[750 1706]。

即使是非整体超视图位置,文字也很清晰。

viewDidLoad View Position x 187.500000 pos y 333.500000 

答案 1 :(得分:0)

日志输出的第一行就出现了问题:

2014-11-25 20:18:10.159 DrawRect[15103:15454664] 
        viewDidLoad View Position x 187.500000 pos y 333.500000 

您已将图层定位在半像素上。半个像素点亮是不可能的,因此您可以获得具有抗锯齿功能的多个像素。这就是“模糊”。

  

我确实检查了积分的位置

嗯,我能说的就是,不,你没有。您的日志证明了它。尝试获取坐标积分甚至 - 积分,使它们不在半像素上,甚至使中心不在半像素上。