07-07-2011, 02:26 AM
#1 (permalink )
Registered Member
Join Date: Jul 2011
Posts: 20
How to right some text on an image(png) in iPhone objective-c?
Hello Friends,
I am very new to the iPhone development and this site. I am working on a project where I have to write some text on a png image and after writing again save it as png. Is it possible through objective-c? Please provide me any sample code or any tutorial for it.
Waiting for replies.
Thanks in advance
07-07-2011, 03:13 AM
#2 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
you can do something like that
Add text to image (UIImage)
Code:
//Add text to UIImage
-(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];
}
__________________
07-07-2011, 04:13 AM
#3 (permalink )
Registered Member
Join Date: Jul 2011
Posts: 20
Thanks
Thanks dany_dev, You provide me the awesomest solution, for which I was looking. Is there any way to wrap the text according to the image size? If possible then please help me on it. Thanks again
07-07-2011, 04:45 AM
#4 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
what you mean exactly? I don't understand, you want to make the text big so that fit the width of your UIImage?
__________________
07-07-2011, 07:38 AM
#5 (permalink )
Registered Member
Join Date: Jul 2011
Posts: 20
Actually My text is more than the width of the image. So, Can I wrap it according to the image width. (Only two lines)
07-07-2011, 07:41 AM
#6 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
you can calculate the size of your text,
this is a cool method to calculate the width of your text with a fixed size
// do something here Blog Archive Balance
Code:
- (CGFloat)calculateTextWidth:(NSString *)text
{
CGSize fullSize = [UIScreen mainScreen].applicationFrame.size;
UIGraphicsBeginImageContext(fullSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// calculate the text size
CGContextSelectFont(context, [fontName UTF8String], fontSize, kCGEncodingMacRoman);
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGContextSetTextDrawingMode(context, kCGTextInvisible);
// measure the text
CGPoint initialTextPosition = CGContextGetTextPosition(context);
CGContextShowTextAtPoint(context, 0, 0, [text cStringUsingEncoding:NSASCIIStringEncoding], text.length);
CGPoint finalTextPosition = CGContextGetTextPosition(context);
UIGraphicsEndImageContext(); // !!!!!
return finalTextPosition.x - initialTextPosition.x;
}
So now, just add fontSize as parameter to the method, and write a while loop to calculate the right maximum size to fix your width UIImage....
__________________
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 355
9 members and 346 guests
apatsufas , chemistry , lendo , leostc , Leslie80 , lzwasyc , MarkC , SamorodovAlex , VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80