سؤال

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];
هل كانت مفيدة؟

المحلول

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;

نصائح أخرى

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.

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];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top