Jump to content

Core Text

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by TakuyaMurata (talk | contribs) at 11:34, 12 January 2008 (Example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Core Text is a CoreFoundation style API in Mac OS X, first introduced in Mac OS X 10.4 Tiger and made public in Mac OS X 10.5 Leopard. Written in C, it replaces the text rendering abilities of the now-deprecated QuickDraw framework in previous versions of Mac OS X.

Example

The following code displays the text "Hello, World!" to the given graphics context.

// Prepare font and color
CTFont font = CTFontCreateWithName (CFSTR ("Times"), 48, NULL); // use the default text matrix

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGFloat components[] = { 0.0, 0.0, 1.0, 1.0 }; // red, green, blue, alpha
CGColorRef color = CGColorCreate (colorSpace, components);
CGColorSpaceRelease (colorSpace);

// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName };
CFTypeRef values[] = { font, color };
CFDictionaryRef attr = CFDictionaryCreate (kCFAllocatorDefault, (const void**) &keys,
   (const void**) &values, sizeof (keys) / sizeof (keys[0]), NULL, NULL);
CFAttributedStringRef attrString = CFAttributedStringCreate (NULL, CFSTR ("Hello, World!"), attr);
CFRelease (attr);

// Draw the string to context
CTLine line = CTLineCreateWithAttributedString (attrString);
CTLineDraw (line, context);
CFRelease (line);

// Cleanup
CFRelease (attrString);
CFRelease (font);
CFRelease (color);