我有一个CALayer,我首先绘制一些东西,然后是文本:
- (void)drawInContext:(CGContextRef)context
{
CGContextSaveGState(context);
// draw things, everything displays correctly ...
CGSize expectedCreditSize = [[gpData.credits stringValue] sizeWithFont:[UIFont
systemFontOfSize:self.fontSize]];
rect = CGRectMake(self.bounds.origin.x, self.bounds.size.height/2,
expectedCreditSize.width, expectedCreditSize.height);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
[self.creditString drawInRect:rect
withFont:[UIFont systemFontOfSize:self.fontSize]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentRight];
CGContextRestoreGState(context);
}
所有图形内容都正确显示,但文本根本没有显示.我究竟做错了什么?
我还尝试将CATextLayer添加为subLayer,但因此我在运行时收到错误消息.
CATextLayer *creditsTextLayer = [[CATextLayer alloc] init];
[creditsTextLayer setFrame:self.frame];
[creditsTextLayer setPosition:self.position];
[creditsTextLayer setString:self.creditString];
[creditsTextLayer setFontSize:self.fontSize];
[creditsTextLayer setAlignmentMode:kCAAlignmentLeft];
[creditsTextLayer setForegroundColor:[[UIColor whiteColor] CGColor]];
[self addSublayer:creditsTextLayer];
唯一有效的是这个解决方案:
CGContextSelectFont (context,
"Helvetica-Bold",
self.fontSize,
kCGEncodingMacRoman);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSetRGBFillColor (context, 0, 1, 0, .5);
CGContextSetRGBStrokeColor (context, 0, 0, 1, 1);
CGContextShowTextAtPoint (context, 40, 0, self.creditString, 9);
但这非常不舒服.
有没有人有想法,我能做些什么才能让我的第一个建议起作用?我错过了显示文字的内容吗?
提前致谢!
编辑:
根据Seamus的回答,我现在正在使用它
- (void)drawInContext:(CGContextRef)context
{
CGContextSaveGState(context);
// draw things like circles and lines,
// everything displays correctly ...
// now drawing the text
UIGraphicsPushContext(context);
CGSize expectedCreditSize = [[gpData.credits stringValue] sizeWithFont:[UIFont
systemFontOfSize:self.fontSize]];
rect = CGRectMake(self.bounds.origin.x, self.bounds.size.height/2,
expectedCreditSize.width, expectedCreditSize.height);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
[self.creditString drawInRect:rect
withFont:[UIFont systemFontOfSize:self.fontSize]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentRight];
UIGraphicsPopContext();
CGContextRestoreGState(context);
}
这意味着我只需按下并弹出文本的图形上下文.这有意义吗?为什么它可以用于绘制其他CGContext的东西?也许有人可以解释……
最佳答案 观察-drawInRect:withFont:lineBreakMode:alignment:不接受CGContextRef参数 – 它绘制到“当前”图形上下文.您没有向我们展示您如何获得您正在绘制的上下文,但通常NSString方法将在-drawRect方法中使用,其中获取的上下文如下所示:
CGContextRef context = UIGraphicsGetCurrentContext();
您可以使用UIGraphicsPushContext()将您的上下文设置为“当前上下文”(但请确保您还调用UIGraphicsPopContext()):
- (void)drawInContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
CGContextSaveGState(context);
// draw things, everything displays correctly ...
CGSize expectedCreditSize = [[gpData.credits stringValue] sizeWithFont:[UIFont
systemFontOfSize:self.fontSize]];
rect = CGRectMake(self.bounds.origin.x, self.bounds.size.height/2,
expectedCreditSize.width, expectedCreditSize.height);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
[self.creditString drawInRect:rect
withFont:[UIFont systemFontOfSize:self.fontSize]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentRight];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}