text1 is a very long string so, I want multiline support.
What do I have to do for this?
int w = img.size.width;
int h = img.size.height;
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, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255,255,255,1);
//CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(-M_PI/4));;
CGContextShowTextAtPoint(context, w/3 + 10, 5, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
If you don’t mind using UIKit methods instead of CGContext methods to draw your text, you can just use
drawInRect:withFont:lineBreakMode:to draw the text, which will handle wrapping for you. Note that you can useUIGraphicsPushContextandUIGraphicsPopContextto go from your existing CGGraphicsRef to the UIKit context, and if you are targeting iOS earler than 4.0 you have to do the UIKit drawing on the main thread.