CATextLayer textcolor is always black

不想你离开。 提交于 2020-01-24 18:14:41

问题


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

回答1:


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;



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/16331345/catextlayer-textcolor-is-always-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!