Skip to content

Instantly share code, notes, and snippets.

@jocafa
Created April 25, 2011 20:38
Show Gist options
  • Save jocafa/941167 to your computer and use it in GitHub Desktop.
Save jocafa/941167 to your computer and use it in GitHub Desktop.
Fancy Strings
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
[[UIColor darkGrayColor] set];
CGContextFillRect(ctx, rect);
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:@"Herp Derp."];
NSRange range = NSMakeRange(2, 4);
CFStringRef fontstr = CFStringCreateWithCString(kCFAllocatorDefault, "Zapfino", kCFStringEncodingASCII);
CTFontRef font = CTFontCreateWithName(fontstr, 18.0, NULL);
[attrstr addAttribute:(NSString*)kCTFontAttributeName value:(id)font range:range];
//CGContextTranslateCTM(ctx, 10.0, 10.0);
CGContextScaleCTM(ctx, 1.0, 1.0);
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1.0, -1.0));
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0.0, 0.0, 320.0, 200.0)];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrstr);
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path.CGPath, NULL);
CFRelease(framesetter);
CTFrameDraw(textFrame, ctx);
CFRelease(textFrame);
UIGraphicsPushContext(ctx);
}
@localshred
Copy link

Oh dear...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment