Skip to content

Instantly share code, notes, and snippets.

@uchuugaka
Forked from ishikawa/gist:23178
Created November 29, 2012 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uchuugaka/4169858 to your computer and use it in GitHub Desktop.
Save uchuugaka/4169858 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