How to write a text on image iOS? - ios

In my app I'm trying to add a text on an image. I've done it using a post in stackoverflow. It's like the following.
+(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
When I'm writing a text using this code, the original image's colour is changed like the following,
Original Image
Output with Text
Can anyone explain why is this happening. and how to fix this??
Thanks in Advance!!

We need to create a image context and apply drawing and get final image from this context. You can try this method. Hope this help you.
+ (UIImage *) addText:(UIImage *)img text:(NSString *)text
{
CGRect rect = [[UIScreen mainScreen] bounds];
// create a context according to image size
UIGraphicsBeginImageContext(rect.size);
// draw image
[img drawInRect:rect];
UIFont* font = [UIFont systemFontOfSize:14.0];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle };
CGRect textRect = CGRectMake(20, 160.0, 280.0, 44);
/// draw text
[text drawInRect:textRect withAttributes:attributes];
// get as image
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

EDIT: This can only be run on the main thread! So it's pretty much useless.
+(UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Here's a new one using Core Graphics that can be used on any thread
+ (UIImage *)imageFromView:(UIView *)view
{
size_t width = view.bounds.size.width*2;
size_t height = view.bounds.size.height*2;
unsigned char *imageBuffer = (unsigned char *)malloc(width*height*8);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colourSpace);
CGContextTranslateCTM(imageContext, 0.0, height);
CGContextScaleCTM(imageContext, 2.0, -2.0);
[view.layer renderInContext:imageContext];
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
UIImage *image = [[UIImage alloc] initWithCGImage:outputImage];
CGImageRelease(outputImage);
CGContextRelease(imageContext);
free(imageBuffer);
return image;
}

I am using this and it works fine for me
- (UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont fontWithName:FONT_BOLD size:20];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[UIColorFromRGB(0x515151) set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Related

draw border around CIImage

I'm looking for a way to add a solid border with Core Image. I have implement custom camera for taking pictures of rectangle shape documents. Now I am getting that documents four coordinates but problem to draw border on CIImage. Please help me.
CIImage *overlay = [CIImage imageWithColor:[CIColor colorWithRed:0 green:1 blue:0 alpha:0.6]];
overlay = [overlay imageByCroppingToRect:image.extent];
overlay = [overlay imageByApplyingFilter:#"CIPerspectiveTransformWithExtent" withInputParameters:#{#"inputExtent":[CIVector vectorWithCGRect:image.extent],#"inputTopLeft":[CIVector vectorWithCGPoint:topLeft],#"inputTopRight":[CIVector vectorWithCGPoint:topRight],#"inputBottomLeft":[CIVector vectorWithCGPoint:bottomLeft],#"inputBottomRight":[CIVector vectorWithCGPoint:bottomRight]}];
return [overlay imageByCompositingOverImage:image];
You can use this methods :
- (UIImage *)addBorderToImage:(UIImage *)image {
CGImageRef bgimage = [image CGImage];
float width = CGImageGetWidth(bgimage);
float height = CGImageGetHeight(bgimage);
// Create a temporary texture data buffer
void *data = malloc(width * height * 4);
// Draw image to buffer
CGContextRef ctx = CGBitmapContextCreate(data,
width,
height,
8,
width * 4,
CGImageGetColorSpace(image.CGImage),
kCGImageAlphaPremultipliedLast);
CGContextDrawImage(ctx, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), bgimage);
//Set the stroke (pen) color
CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
//Set the width of the pen mark
CGFloat borderWidth = (float)width*0.05;
CGContextSetLineWidth(ctx, borderWidth);
//Start at 0,0 and draw a square
CGContextMoveToPoint(ctx, 0.0, 0.0);
CGContextAddLineToPoint(ctx, 0.0, height);
CGContextAddLineToPoint(ctx, width, height);
CGContextAddLineToPoint(ctx, width, 0.0);
CGContextAddLineToPoint(ctx, 0.0, 0.0);
//Draw it
CGContextStrokePath(ctx);
// write it to a new image
CGImageRef cgimage = CGBitmapContextCreateImage(ctx);
UIImage *newImage = [UIImage imageWithCGImage:cgimage];
CFRelease(cgimage);
CGContextRelease(ctx);
// auto-released
return newImage;
}
Calling this method:
UIImage *updatedIMG = [self addBorderToImage:[[UIImage alloc] initWithCIImage:overlay]]

iOS UISegmentedControl with image overlaps by tint color

I have some problem with UISegmentedControl with image. When I put image with text on it in segment tintcolor paints my image and the text.
Can any help with it?
setting up UIImage in Segment
UIImage *tmp = [UIImage imageNamed:#"UISegmentedControl-active"];
UIImage *imageWithText = [UIImage imageFromImage:tmp string:#"20" color:[UIColor whiteColor]];
[self.segmentedControl setImage:imageWithText forSegmentAtIndex:0];
imageFromImage code:
+ (id) imageFromImage:(UIImage*)image string:(NSString*)string color:(UIColor*)color
{
UIFont *font = [UIFont systemFontOfSize:16.0];
CGSize expectedTextSize = [string sizeWithAttributes:#{NSFontAttributeName: font}];
int width = image.size.width + 5;
int height = MAX(expectedTextSize.height, image.size.width);
CGSize size = CGSizeMake((float)width, (float)height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
int fontTopPosition = (height - expectedTextSize.height) / 2;
CGPoint textPoint = CGPointMake(width + 5, fontTopPosition);
// Images upside down so flip them
//CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
//CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ {0, (height - image.size.height) / 2}, {image.size.width, image.size.height} }, [image CGImage]);
[string drawAtPoint:textPoint withAttributes:#{NSFontAttributeName: font}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Image goes blue(default tintcolor).
Anyway, if anyone know how to do segmented control with circular(not rounded) segments(buttons), offer your solution.

drawAtPoint not working after converting from CGContextShowTextAtPoint

as CGContextShowTextAtPoint is deprecated in iOS 7, i would like to change CGContextShowTextAtPoint to drawAtPoint but the text is outputting.
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = 32;
int h = 32;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
//char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
// CGContextSelectFont(context, "Montserrat-Regular",12, kCGEncodingMacRoman);
// CGContextShowTextAtPoint(context,9,12,text, strlen(text));
//
[text1 drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:#"Montserrat-Regular" size:12]];
CGImageRef imgCombined = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
CGImageRelease(imgCombined);
return retImage;
}
ok got it work on
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
UIGraphicsBeginImageContext(CGSizeMake(36, 36));
CGRect aRectangle = CGRectMake(0,0, 36.0f,36.0f);
[img drawInRect:aRectangle];
[text1 drawInRect : CGRectMake(0, 10, 36, 36)
withFont : [UIFont fontWithName:#"Montserrat-Regular" size:12]
lineBreakMode : NSLineBreakByTruncatingTail
alignment : NSTextAlignmentCenter ];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}

DrawText on Image with text Adjusting to the size of the Image

I am drawing the text on the image. text is drawn without any problem , but my text is dynamic sometimes its more characters or sometimes its less.
From below image you can see "This is long message more than 320 width of image , in that of image doesn't come .
I want all the Message to be displayed in the next line, because my Message is dynamic and i want to display it in 320 *480 size of image, here i am displaying small image just for testing purpose .
This is the Code for drawing Text :
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont (context, // 3
"Helvetica-Bold",
15,
kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
Try This code:
lblText.text=[NSString stringWithFormat:#"Put Your Text"];
lblText.font = [UIFont fontWithName:#"Helvetica" size:10.0f];
CGPoint point=lblText.center;
imgview.image =[self drawText:lblText.text inImage:imgview.image atPoint:point];
//Save it in Document directory for Checking Purpose...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *image1 = imgview.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:savedImagePath atomically:NO];
-(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point
{
UIFont *font =lblText.font;;
if ([UIScreen instancesRespondToSelector:#selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(imgview.frame.size, NO, 0.0f);
} else {
UIGraphicsBeginImageContext(imgview.frame.size);
}
[image drawInRect:CGRectMake(0,0,imgview.frame.size.width,imgview.frame.size.height)];
CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);//Set Frame as per your Requirement
[lblText.textColor set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
It will Help.
Use a UIImageView to display your image and add a UILabel as the subview of UIImageView.
1.configure your UILabel first:
[yourLabel setNumberOfLines:0];
[yourLabel setLineBreakMode:NSLineBreakByWordWrapping];
2.get your label frame with the contents length
- (CGRect)labelFrameWithText:(NSString *)text
{
CGRect rect;
// the font of your text
UIFont *font = [UIFont systemFontOfSize:15.0];
NSDictionary *attributes = #{NSFontAttributeName: font};
// the first parametric CGSize is the max size that the rect's size can be
CGRect boundingRect = [title boundingRectWithSize:CGSizeMake(youImageWidth, 100.0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
//the rect of the UILabel
//This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
rect = CGRectMake(yourLabelOriginX,
yourLabelOriginY,
ceil(boundingRect.size.width),
ceil(boundingRect.size.height));
return rect;
}
3.change your label's frame and setText
------- old version ---------
CGSize contentSize = [content sizeWithFont:font
constrainedToSize:CGSizeMake(maxWidth, maxHeight)
lineBreakMode: NSLineBreakByWordWrapping];

Drawing text with accented characters (i.e. French Text)

I want to draw text on an image. I've tried using CGContextShowTextAtPoint and NSString drawAtPoint, but they both fail to show text with accented characters (i.e. if the text is in French). Basically I want to be able to show unicode characters. Any hints/solutions?
My solution with Core-Graphics (UIImage category method):
- (UIImage *)overlayText:(NSString *)overlayText withFontName:(NSString *)fontName andFontSize:(CGFloat)fontSize {
// Refererence:
// http://iphonesdksnippets.com/post/2009/05/05/Add-text-to-image-%28UIImage%29.aspx
int w = self.size.width;
int h = self.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), self.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char *charText = (char *)[overlayText cStringUsingEncoding:NSASCIIStringEncoding];
char *charFontName = (char *)[fontName cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, charFontName, fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
// rotate text
// CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(-M_PI / 4));
CGFloat textWidth = [overlayText calculateCGTextWidthWithFont:charFontName size:fontSize];
CGFloat xTextPosition = MAX(10.0, w - textWidth - 10.0); // 10.0 inset
CGFloat yTextPosition = 10.0;
CGContextShowTextAtPoint(context, xTextPosition, yTextPosition, charText, strlen(charText));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
My solution with NSString (UIImage category method):
- (UIImage *)overlayText:(NSString *)overlayText withFontName:(NSString *)fontName andFontSize:(CGFloat)fontSize {
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
char *charFontName = (char *)[fontName cStringUsingEncoding:NSASCIIStringEncoding];
CGFloat textWidth = [overlayText calculateCGTextWidthWithFont:charFontName size:fontSize];
CGFloat xTextPosition = MAX(10.0, self.size.width - textWidth - 10.0); // 10.0 margin/inset
CGFloat yTextPosition = self.size.height - fontSize - 10.0; // 10.0 margin/inset
UIGraphicsBeginImageContext(self.size);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
[[UIColor whiteColor] set];
[overlayText drawAtPoint:CGPointMake(xTextPosition, yTextPosition) withFont:font];
UIImage *overlayTextImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return overlayTextImage;
}
My solution with Core-Text (UIImage category method):
- (UIImage *)overlayText:(NSString *)overlayText withFontName:(NSString *)fontName andFontSize:(CGFloat)fontSize {
char *charFontName = (char *)[fontName cStringUsingEncoding:NSASCIIStringEncoding];
CGFloat textWidth = [overlayText calculateCGTextWidthWithFont:charFontName size:fontSize];
CGFloat xTextPosition = 10.0; // MAX(10.0, self.size.width - textWidth - 10.0); // 10.0 margin/inset
CGFloat yTextPosition = self.size.height - fontSize - 10.0; // 10.0 margin/inset
UIGraphicsBeginImageContext(self.size);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
// flip the coordinate system
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// create an attributed string
CFMutableAttributedStringRef attributedOverlayText = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
if (overlayText != nil)
CFAttributedStringReplaceString(attributedOverlayText, CFRangeMake(0, 0), (CFStringRef)overlayText);
CFAttributedStringSetAttribute(attributedOverlayText, CFRangeMake(0, CFAttributedStringGetLength(attributedOverlayText)), kCTForegroundColorAttributeName, [[UIColor whiteColor] CGColor]);
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
CFAttributedStringSetAttribute(attributedOverlayText, CFRangeMake(0, CFAttributedStringGetLength(attributedOverlayText)), kCTFontAttributeName, ctFont);
CFRelease(ctFont);
// draw attributed string using CTLineDraw
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attributedOverlayText);
CGContextSetTextPosition(context, xTextPosition, yTextPosition);
CTLineDraw(line, context);
UIImage *overlayTextImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return overlayTextImage;
}
I problem with the above solution is that I'm essentially not getting the proper "width" for the text.
Here's the solution that worked for me (using Core Text):
- (UIImage *)overlayText:(NSString *)overlayText withFontName:(NSString *)fontName andFontSize:(CGFloat)fontSize {
// Develope's Note:
// Problem with implementing a solution using CG is that it doesn't support unicode character drawing i.e French
// Begin new image context
UIGraphicsBeginImageContext(self.size);
// Draw image
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
// Flip the coordinate system
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Create an attributed string
CFMutableAttributedStringRef attributedOverlayText = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
if (overlayText != nil)
CFAttributedStringReplaceString(attributedOverlayText, CFRangeMake(0, 0), (__bridge CFStringRef)overlayText);
// Set text color
CFAttributedStringSetAttribute(attributedOverlayText,
CFRangeMake(0, CFAttributedStringGetLength(attributedOverlayText)),
kCTForegroundColorAttributeName,
[[UIColor whiteColor] CGColor]);
// Set text font
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)fontName, fontSize, NULL);
CFAttributedStringSetAttribute(attributedOverlayText,
CFRangeMake(0, CFAttributedStringGetLength(attributedOverlayText)),
kCTFontAttributeName,
ctFont);
CFRelease(ctFont);
// Set text alignment (paragraph style)
CTTextAlignment alignment = kCTRightTextAlignment;
CTParagraphStyleSetting settings[] = {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
CFAttributedStringSetAttribute(attributedOverlayText,
CFRangeMake(0, CFAttributedStringGetLength(attributedOverlayText)),
kCTParagraphStyleAttributeName,
paragraphStyle);
CFRelease(paragraphStyle);
// Create framesetter with the attributed text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedOverlayText);
CFRange stringRange = CFRangeMake(0, CFAttributedStringGetLength(attributedOverlayText));
CGSize fitRange = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,
CFRangeMake(0, 0),
NULL,
CGSizeMake(self.size.width - (2 * 10.0), CGFLOAT_MAX),
NULL);
// Developer's Note:
// We can't use fontSize for the framesetter's height.
// We can't use fitRange.height in the bounding rectangle, because then we won't get the correct text alignment
// Calculate the width (and correspondingly the xTextPosition)
CGRect boundingRect = CGRectMake(10.0,
10.0,
self.size.width - (2 * 10.0),
fitRange.height);
CGMutablePathRef boundingPath = CGPathCreateMutable();
CGPathAddRect(boundingPath, NULL, boundingRect);
// Draw attributed text using CTFrameDraw
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, stringRange, boundingPath, NULL);
CTFrameDraw(frame, context);
CFRelease(framesetter);
// Draw boundary (debugging purposes only)
if (NO) {
// Inner boundary
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextStrokeRect(context, CGPathGetBoundingBox(boundingPath));
// Outer boundary
CGMutablePathRef controlBoundaryPath = CGPathCreateMutable();
CGPathAddRect(controlBoundaryPath, NULL, CGRectMake(0, 0, self.size.width, self.size.height));
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextStrokeRect(context, CGPathGetBoundingBox(controlBoundaryPath));
CFRelease(controlBoundaryPath);
}
// Draw attributed string using CTLineDraw
// (Doesn't work if we want to right align the text)
// CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attributedOverlayText);
// CGContextSetTextPosition(context, xTextPosition, yTextPosition);
// CTLineDraw(line, context);
UIImage *overlayTextImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return overlayTextImage;
}
Hope this helps someone.

Resources