私は最初にいくつかのものを描き、次にテキストを描く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 も追加しようとしましたが、実行時にエラー メッセージが表示されます。
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);
しかし、これは非常に不快です。
私の最初の提案の1つを機能させるために何ができるか、誰かアイデアはありますか? テキストを表示するために何か不足していますか?
前もって感謝します!
編集:
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 のものを描画するために機能するのはなぜですか? 誰かが説明してくれるかもしれません…</p>