Draw Text
suggest changeThis needs Core Text (#import <CoreText/CoreText.h>). Call it when a graphics context is current. stringRef is a __bridge cast, so it must not be CFReleased. currentText is created here, so it must be released. The translate moves the origin to (200, 300), and the scale of (2, -2) flips the text vertically.
#import <CoreText/CoreText.h>
NSString *textToDraw = @"Welcome to the world Of IOS";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGRect frameRect = CGRectMake(0, 0, 300, 100);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 200, 300);
CGContextScaleCTM(currentContext, 2, -2);
CTFrameDraw(frameRef, currentContext);
CFRelease(frameRef);
CFRelease(currentText);
CFRelease(framesetter);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents