Skip to content

Instantly share code, notes, and snippets.

@fra3il

fra3il/0.m Secret

Last active January 27, 2016 02:15
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 fra3il/0a1884c305de52a20ddc to your computer and use it in GitHub Desktop.
Save fra3il/0a1884c305de52a20ddc to your computer and use it in GitHub Desktop.
[WP/153] CGContextShowTextAtPoint vs. CoreText
#import <CoreText/CoreText.h>
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGContextSetTextDrawingMode(context, kCGTextFill);
// CGContextShowTextAtPoint 를 사용하는 경우
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSelectFont(context, "Helvetica", 10.0, kCGEncodingMacRoman);
NSString *textString = [[NSString alloc] initWithString:@"CGContextShowTextAtPoint"];
CGContextShowTextAtPoint(context, 0, 10, [textString UTF8String], strlen([textString UTF8String]));
// CoreText 를 사용하는 경우
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"CoreText"];
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 10.0, NULL);
[string addAttribute:(id)kCTFontAttributeName value:(__bridge id)helvetica range:NSMakeRange(0, [string length])];
[string addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor blackColor].CGColor range:NSMakeRange(0, [string length])];
CTFramesetterRef textFramesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);
CGMutablePathRef textPath = CGPathCreateMutable();
CGPathAddRect(textPath, NULL, CGRectMake(0, 10, 100, 20));
CTFrameRef textFrame = CTFramesetterCreateFrame(textFramesetter, CFRangeMake(0, 0), textPath, NULL);
CTFrameDraw(textFrame, context);
CFRelease(textFrame);
CGPathRelease(textPath);
CFRelease(textFramesetter);
CFRelease(helvetica);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment