The following code displays the text "Hello, World!" to the given graphics context in Objective-C.
// Prepare font
CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 48, NULL);
// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, CFSTR("Hello, World!"), attr);
CFRelease(attr);
// Draw the string
CTLineRef line = CTLineCreateWithAttributedString(attrString);
CGContextSetTextMatrix(context, CGAffineTransformIdentity); //Use this one when using standard view coordinates
//CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); //Use this one if the view's coordinates are flipped
CGContextSetTextPosition(context, 10, 20);
CTLineDraw(line, context);
// Clean up
CFRelease(line);
CFRelease(attrString);
CFRelease(font);