Skip to content

Instantly share code, notes, and snippets.

@rsms
Created January 6, 2011 17:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsms/768203 to your computer and use it in GitHub Desktop.
Save rsms/768203 to your computer and use it in GitHub Desktop.
Some ramblings and cut-outs from wwdc presentation on CoreText
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
CTFontRef font = CTFontCreateWithName(CFSTR("LucidaGrande"), 11.0, NULL);
CFTypeRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef attributes =
CFDictionaryCreate(kCFAllocatorDefault,
(const void**)keys,
(const void**)values,
sizeof(keys) / sizeof(keys[0]), // CFIndex numValues
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attributedString =
CFAttributedStringCreate(kCFAllocatorDefault,
CFSTR("Hello internet"),
attributes);
// ---------
// Drawing a simple line
CTLineRef line = CTLineCreateWithAttributedString(attributedString);
CGContextRef g = NULL; // TODO
CGContextSetTextMatrix(g, CGAffineTransformIdentity);
// todo: determine text position, perhaps by measuring line
CGContextSetTextPosition(g, 123.0, 456.0);
CTLineDraw(line, g);
// --------
// Drawing a frame (simple paragraph)
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, CGAffineTransformIdentity,
CGRectMake(0.0, 0.0, 100.0, 100.0));
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(attributedString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0),
path, NULL);
CTFrameDraw(frame, g);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment