CATextLayer textcolor始终是黑色的

我正在尝试添加一个CATextLayerUITableViewCell 。 问题是,当我已经设置了它的foregroundColor时,文本呈现为黑色。 有人能告诉我我失踪了吗? 谢谢。

 CATextLayer *buyPrice_port = [CATextLayer layer]; buyPrice_port.frame = CGRectMake(144, 42, 76, 21); buyPrice_port.font = (__bridge CFTypeRef)([UIFont boldSystemFontOfSize:18]); buyPrice_port.foregroundColor = [UIColor colorWithRed:0 green:190/255.0 blue:191/255.0 alpha:1.0].CGColor; buyPrice_port.alignmentMode = kCAAlignmentCenter; buyPrice_port.string = @"BAC"; [self.contentView.layer addSublayer:buyPrice_port]; 

这个奇怪的问题是由于我设置字体的方式。 相反,它应该像这样设置:

 buyPrice.font = CFBridgingRetain([UIFont boldSystemFontOfSize:18].fontName); buyPrice.fontSize = 18; 

只是想我会去修改这里的信息。 你的代码不工作的原因是UIFont不能被铸造成CGFontRef(NSFont可以在Mac OS X下)。 CATextLayer的字体属性可以预期在Mac OS下的字体,但是在iOS下它只能使用字体的名称作为string,所以你可以只键入

 layer.font = (__bridge CFTypeRef)@"Futura" 

你不应该在NSString上使用CFBridgingRetain。 你将负责通过调用CFRelease来释放它。

使用CPTMutableTextStyle来更改CPTTextLayer的字体和颜色,希望它可以帮助…..

 static CPTMutableTextStyle *whiteText = nil; if ( !whiteText ) { whiteText = [[CPTMutableTextStyle alloc] init]; whiteText.color = [CPTColor whiteColor]; } CPTTextLayer *newLayer = [[[CPTTextLayer alloc] initWithText:titleString style:whiteText] autorelease];