Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Typesetting a simple paragraph by using Core Text
- (void) drawText: (NSRect) theRect {
const CTFramesetterRef framesetter =
// 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);
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment