Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-07-2011, 02:26 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 20
Sanchit is on a distinguished road
Default 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
Sanchit is offline   Reply With Quote
Old 07-07-2011, 03:13 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

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];

}
__________________
dany_dev is offline   Reply With Quote
Old 07-07-2011, 04:13 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 20
Sanchit is on a distinguished road
Default 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
Sanchit is offline   Reply With Quote
Old 07-07-2011, 04:45 AM   #4 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

what you mean exactly? I don't understand, you want to make the text big so that fit the width of your UIImage?
__________________
dany_dev is offline   Reply With Quote
Old 07-07-2011, 07:38 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 20
Sanchit is on a distinguished road
Default

Actually My text is more than the width of the image. So, Can I wrap it according to the image width. (Only two lines)
Sanchit is offline   Reply With Quote
Old 07-07-2011, 07:41 AM   #6 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

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....
__________________
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, objective - c, text, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:34 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0