Skip to content

Instantly share code, notes, and snippets.

@darcyliu
Last active December 13, 2016 09:40
Show Gist options
  • Save darcyliu/8168894 to your computer and use it in GitHub Desktop.
Save darcyliu/8168894 to your computer and use it in GitHub Desktop.
Simple CoreText with CTFrameDraw
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip the coordinate system
// Make an attributed string
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello CoreText!"];
CFAttributedStringRef attributedStringRef = (CFAttributedStringRef)attributedString;
// Simple CoreText with CTFrameDraw
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedStringRef);
CGPathRef path = CGPathCreateWithRect(self.bounds,NULL);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0),path,NULL);
CTFrameDraw(frame, context);
// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
NSDictionary *attrs = @{NSFontAttributeName: [UIFont systemFontOfSize:20],
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
};
// Make an attributed string
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello CoreText!" attributes:attrs];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment