4

私は最初にいくつかのものを描き、次にテキストを描く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>

4

1 に答える 1

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();
}
于 2013-04-04T18:40:28.173 に答える