I have a CALayer on which I draw the material first and then the text:
- (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); }
All graphic data is displayed correctly, but the text is missing. What am I doing wrong?
I also tried adding CATextLayer as a sublayer, but so I get an error at runtime.
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];
The only thing that worked was the solution:
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);
But it is very inconvenient.
Does anyone have an idea what I can do to make one of my first suggestions work? Am I missing something to display text?
Thanks in advance!
change
based on Seamus answer, I got this job now
- (void)drawInContext:(CGContextRef)context { CGContextSaveGState(context);
This means that I just click and click on the graphics context for the text. Does this make sense? Why does it work to draw other CGContext materials? Maybe someone can explain ...