我正在使用UIBezierPath
. 我想在上面绘制它,CGLayer
以便在某些事件(通过调用setNeedsDisplay
)之后缓存圆圈并在其上绘制文本。我应该如何在 CGContextRef 上绘制 UIBezierPath。我的代码如下
- (void)drawRect:(CGRect)rect
{
// Drawing code
static CGLayerRef sTextLayer = NULL;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect textBounds = CGRectMake(0, 0, 200, 100);
if (sTextLayer == NULL) {
sTextLayer = CGLayerCreateWithContext(ctx, textBounds.size, NULL);
CGContextRef textCtx = CGLayerGetContext(sTextLayer);
CGContextSetRGBFillColor (textCtx, 1.0, 0.0, 0.0, 1);
UIGraphicsPushContext(textCtx);
// Draw circle
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:textBounds];
[[UIColor blackColor] setFill];
circle.lineWidth = 2.0;
[circle fill];
UIGraphicsPopContext();
}
if (self.drawString) {
UIFont *font = [UIFont systemFontOfSize:13.0];
NSString *string = @"HAPPY BIRTHDAY";
[string drawInRect:textBounds withFont:font];
}
}