CATextLayer textcolor is always black

2019-07-18 04:53发布

I am trying to add a CATextLayer to UITableViewCell. Problem is that text is rendering as black while I have already set its foregroundColor. Can someone tell me what I am missing? Thanks.

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条回答
地球回转人心会变
2楼-- · 2019-07-18 05:09

This strange problem was due to the way I was setting my font. Instead it should be set like this:

buyPrice.font = CFBridgingRetain([UIFont boldSystemFontOfSize:18].fontName);
buyPrice.fontSize = 18;
查看更多
相关推荐>>
3楼-- · 2019-07-18 05:14

Use CPTMutableTextStyle for changing the font and colour of CPTTextLayer, hope it may helps.....

static CPTMutableTextStyle *whiteText = nil;
if ( !whiteText ) {
    whiteText       = [[CPTMutableTextStyle alloc] init];
    whiteText.color = [CPTColor whiteColor];
}

    CPTTextLayer *newLayer = [[[CPTTextLayer alloc] initWithText:titleString
                                                           style:whiteText] autorelease];
查看更多
女痞
4楼-- · 2019-07-18 05:31

Just thought I would chime in to correct the information that is here. The reason for your code not working is that UIFont cannot be casted to CGFontRef (NSFont can however under Mac OS X). CATextLayer's font property can expect a font under Mac OS but under iOS it can only use the name of the font as a string, so you could just type

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

You should not be using CFBridgingRetain on the NSString. You would be responsible for releasing it by calling CFRelease.

查看更多
登录 后发表回答