As @matt has noted, this is not the Cocoa code, the origin of the coordinate system is "bottom-left" on Cocoa, and UIGraphicsGetCurrentContext()
does not exist…
Anyway you need to set the text matrix of the context at least to CGAffineTransformIdentity
. But as CGContextSetTextPosition
don't set the position for CTDrawLine
, you need to translate the text matrix to your desired position.
- (void)drawRect:(NSRect)dirtyRect
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CFStringRef font_name = CFStringCreateWithCString(NULL, "Courier", kCFStringEncodingMacRoman);
CTFontRef font = CTFontCreateWithName(font_name, 36.0, NULL);
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef font_attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)&keys, (const void **)&values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFRelease(font_name);
CFRelease(font);
int x = 10;
int y = 10;
CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, CFSTR("Hello World!"), font_attributes);
CTLineRef line = CTLineCreateWithAttributedString(attr_string);
// You need to set the text matrix at least to CGAffineTransformIdentity
// Here we translate to the desired position
CGContextSetTextMatrix(context, CGAffineTransformMakeTranslation(x,y));
CTLineDraw(line, context);
CFRelease(line);
CFRelease(attr_string);
CGContextRestoreGState(context);
}