CATextLayer textcolor始终为黑色

时间:2013-05-02 06:02:24

标签: ios uitableview catextlayer

我正在尝试向CATextLayer添加UITableViewCell。问题是文本渲染为黑色,而我已经设置了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];

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

以为我会想要纠正这里的信息。您的代码无法正常工作的原因是UIFont无法转换为CGFontRef(NSFont可以在Mac OS X下使用)。 CATextLayer的字体属性可以在Mac OS下使用字体,但在iOS下它只能使用字体名称作为字符串,因此您只需输入

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

您不应该在NSString上使用CFBridgingRetain。您将负责通过致电CFRelease释放它。

答案 2 :(得分:-1)

使用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];
相关问题