Reputation: 14404
I'm having trouble drawing a string in a custom CALayer. The drawInContext method is called but nothing is visible on the screen. I can fill the layer with a solid colour but can't seem to draw a string onto it.
Any suggestions? Thanks.
- (void)drawInContext:(CGContextRef)ctx
{
CGContextSaveGState(ctx);
NSString *myString = @"Hello World";
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:14], NSFontAttributeName, nil];
[myString drawAtPoint:NSMakePoint(0,0) withAttributes:attr];
CGContextRestoreGState(ctx);
}
Upvotes: 0
Views: 2240
Reputation: 14404
I didn't want to use Quartz to draw a string but I guess I have to. So here is what I ended up doing
NSString *string = @"Hello world";
CGContextSelectFont (ctx,"Helvetica", 24, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
CGContextShowTextAtPoint (ctx, 40, 40, [string UTF8String], (int)[string length]);
Alternative solution
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext *currentContext = [NSGraphicsContext graphicsContextWithGraphicsPort:theContext flipped:NO];
[NSGraphicsContext setCurrentContext:currentContext];
//Draw here
[NSGraphicsContext restoreGraphicsState];
Upvotes: 3
Reputation: 1751
You'd probably want to use a CATextLayer. Get the parent layer, add your text to the TextLayer, then use -addSublayer to add the TextLayer to the parent layer.
Upvotes: 0