-
-
Save fra3il/0a1884c305de52a20ddc to your computer and use it in GitHub Desktop.
[WP/153] CGContextShowTextAtPoint vs. CoreText
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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