Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created November 9, 2008 02:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ishikawa/23178 to your computer and use it in GitHub Desktop.
Save ishikawa/23178 to your computer and use it in GitHub Desktop.
Typesetting a simple paragraph by using Core Text
- (void) drawText: (NSRect) theRect {
const CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_text);
// Initialize a rectangular path.
const CGMutablePathRef path = CGPathCreateMutable();
const CGRect rect = CGRectInset(NSRectToCGRect([self bounds]), 5.0, 5.0);
CGPathAddRect(path, NULL, rect);
// Create the frame and draw it into the graphics context
const CTFrameRef frame =
CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(path);
// Initialize a graphics context and set the text matrix to a known value
CGContextRef context = (CGContextRef)
[[NSGraphicsContext currentContext] graphicsPort];
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CTFrameDraw(frame, context);
CFRelease(frame);
CFRelease(framesetter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment