Drawn text on CALayer does not appear or crashes at runtime

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); // 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); } 

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 ...

+4
source share
1 answer

Note that -drawInRect:withFont:lineBreakMode:alignment: does not accept the CGContextRef parameter - it refers to the "current" graphic context. You do not show us how you got the context you are drawing, but usually this NSString method will be used in the -drawRect method with the context obtained as:

 CGContextRef context = UIGraphicsGetCurrentContext(); 

You can make your context "current context" with UIGraphicsPushContext() (but make sure you also call 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(); } 
+2
source

All Articles