Created
March 13, 2015 17:12
-
-
Save timgcarlson/70fafde5eed5e536facb to your computer and use it in GitHub Desktop.
Method to draw text to the current Core Graphics context (missing vertical alignment ability)
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
- (void)drawText:(NSString *)textToDraw inFrame:(CGRect)frameRect withFont:(UIFont *)originalFont textColor:(UIColor *)textColor alignment:(PDFTextAlignment)alignment verticalAlignment:(PDFTextVerticalAlignment)verticalAlignment { | |
if (!textToDraw) { | |
// If nil, give it an empty value to draw | |
textToDraw = @""; | |
} | |
// Prepare font | |
CTFontRef font = [self ctFontRefFromUIFont:originalFont]; | |
CGColorRef color = textColor.CGColor; | |
// Paragraph | |
CTTextAlignment ctAlignment; | |
switch (alignment) { | |
case PDFTextAlignmentLeft: | |
ctAlignment = kCTTextAlignmentLeft; | |
break; | |
case PDFTextAlignmentCenter: | |
ctAlignment = kCTTextAlignmentCenter; | |
break; | |
case PDFTextAlignmentRight: | |
ctAlignment = kCTTextAlignmentRight; | |
break; | |
case PDFTextAlignmentJustified: | |
ctAlignment = kCTTextAlignmentJustified; | |
break; | |
default: | |
ctAlignment = kCTTextAlignmentLeft; | |
break; | |
} | |
CTParagraphStyleSetting settings[] = { | |
{kCTParagraphStyleSpecifierAlignment, sizeof(ctAlignment), &ctAlignment}, | |
}; | |
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0])); | |
// Create an attributed string | |
CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName, kCTParagraphStyleAttributeName }; | |
CFTypeRef values[] = { font, color, paragraphStyle }; | |
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, | |
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | |
CFStringRef stringRef = (__bridge CFStringRef)textToDraw; | |
// Prepare the text using a Core Text Framesetter. | |
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, attr); | |
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); | |
CGMutablePathRef framePath = CGPathCreateMutable(); | |
CGPathAddRect(framePath, NULL, frameRect); | |
// Get the frame that will do the rendering. | |
CFRange currentRange = CFRangeMake(0, 0); | |
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); | |
CGPathRelease(framePath); | |
// Get the graphics context. | |
CGContextRef currentContext = UIGraphicsGetCurrentContext(); | |
// Put the text matrix into a known state. This ensures | |
// that no old scaling factors are left in place. | |
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); | |
// Core Text draws from the bottom-left corner up, so flip | |
// the current transform prior to drawing. | |
// Modify this to take into consideration the origin. | |
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); | |
CGContextScaleCTM(currentContext, 1.0, -1.0); | |
// Draw the frame. | |
CTFrameDraw(frameRef, currentContext); | |
// Add these two lines to reverse the earlier transformation. | |
CGContextScaleCTM(currentContext, 1.0, -1.0); | |
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); | |
// TODO: Confirm that everything is released that needs to be! | |
CFRelease(frameRef); | |
CFRelease(stringRef); | |
CFRelease(framesetter); | |
CFRelease(font); | |
CFRelease(paragraphStyle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment