2

我正在尝试添加一个CATextLayerUITableViewCell. 问题是文本呈现为黑色,而我已经设置了它的前景颜色。有人能告诉我我错过了什么吗?谢谢。

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];
4

3 回答 3

3

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

buyPrice.font = CFBridgingRetain([UIFont boldSystemFontOfSize:18].fontName);
buyPrice.fontSize = 18;
于 2013-05-02T09:19:05.783 回答
2

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

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

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

于 2013-06-03T06:25:52.873 回答
-2

使用 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];
于 2014-04-14T06:22:42.670 回答