如何在drawAtPoint中改变NSString的颜色

我在这里有大量的代码,它用一个字符的string绘制一个块:

CGContextDrawImage(context, CGRectMake([blok getLocation].x * xunit, [blok getLocation].y * yunit, 40, 40), [blok getImage].CGImage); [[blok getSymbol] drawAtPoint:CGPointMake([blok getLocation].x * xunit+15, [blok getLocation].y * yunit) withFont:[UIFont fontWithName:@"Helvetica" size:24]]; 

它工作正常,但我一直在做一些布局的改变,现在我需要它,所以绘制的string将是白色的。 使用设置填充颜色和笔触颜色的方法没有做任何事情。 有没有其他的方法来做到这一点?

你有没有尝试过:

 CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor); 

例如:

 CGContextDrawImage(context, CGRectMake([blok getLocation].x * xunit, [blok getLocation].y * yunit, 40, 40), [blok getImage].CGImage); CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor); [[blok getSymbol] drawAtPoint:CGPointMake([blok getLocation].x * xunit+15, [blok getLocation].y * yunit) withFont:[UIFont fontWithName:@"Helvetica" size:24]]; 

在属性中设置前景色并使用具有属性function的绘制

 NSDictionary *attributes = [NSDictionary dictionaryWithObjects: @[font, [UIColor whiteColor]] forKeys: @[NSFontAttributeName, NSForegroundColorAttributeName]]; [string drawInRect:frame withAttributes:attributes]; 

这是我用于绘制标签的内容:

 - (void)_drawLabel:(NSString *)label withFont:(UIFont *)font forWidth:(CGFloat)width atPoint:(CGPoint)point withAlignment:(UITextAlignment)alignment color:(UIColor *)color { // obtain current context CGContextRef context = UIGraphicsGetCurrentContext(); // save context state first CGContextSaveGState(context); // obtain size of drawn label CGSize size = [label sizeWithFont:font forWidth:width lineBreakMode:UILineBreakModeClip]; // determine correct rect for this label CGRect rect = CGRectMake(point.x, point.y - (size.height / 2), width, size.height); // set text color in context CGContextSetFillColorWithColor(context, color.CGColor); // draw text [label drawInRect:rect withFont:font lineBreakMode:UILineBreakModeClip alignment:alignment]; // restore context state CGContextRestoreGState(context); }