我做了以下操作:
上面的代码不能正常工作。为什么呢?
如果我改为以下方式,则可以正常工作:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext ( ctx );
CGRect r = CGRectMake( 500, 300, 200, 100 );
NSString *text = [[NSString alloc] initWithString:@"raaaaaaaa!"];
UIColor *color = [ UIColor colorWithRed: (200.0f) green: (100.0) blue:(200.0f) alpha: 1.0f ];
[color set];
[ text drawInRect: r withFont:[UIFont systemFontOfSize:50] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentLeft ];
[text release];
UIGraphicsPopContext();
}
}
}
上面的代码不能正常工作。为什么呢?
如果我改为以下方式,则可以正常工作:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext ( ctx );
CATextLayer *label = [[CATextLayer alloc] init];
[label setFont:@"Didot"];
[label setFontSize:50];
[label setFrame:CGRectMake( 400, 300, 400, 100 )];
[label setString:@"Helloooooooooooooooooo"];
[label setAlignmentMode:kCAAlignmentCenter];
[label setForegroundColor:[[UIColor blueColor] CGColor]];
[layer addSublayer:label];
[label release];
UIGraphicsPopContext();
}
}
什么是差异,为什么drawInRect无效?
谢谢。
编辑 所以,我没有正确使用UIColor,所以我进行了更改。
UIColor *color = [ UIColor colorWithRed: (200.0f) green: (100.0) blue:(200.0f) alpha: 1.0f ];
为此
UIColor *color = [UIColor colorWithRed:200 / 255.0 green:100 / 255.0 blue: 200 / 255.0 alpha: 1.0];
仍然没有成功,使用drawInRect时我无法看到任何文本。