我有一个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的东西?也许有人可以解释......
答案 0 :(得分:2)
观察-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();
}