How can we change color of a text programmatically ?

Posted by user297535 on Stack Overflow See other posts from Stack Overflow or by user297535
Published on 2010-03-24T03:58:18Z Indexed on 2010/03/24 4:03 UTC
Read the original article Hit count: 474

Filed under:
|
|
|

My code is -(UIImage *)addText:(UIImage *)img text:(NSString *)text1{ 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, 2);

CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));


CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

return [UIImage imageWithCGImage:imageMasked];

} -(UIImage *)addText:(UIImage *)img text:(NSString *)text1{ 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, 2);

CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));


CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

return [UIImage imageWithCGImage:imageMasked];

}

How can we change the color of the text programmatically? Answers will be greatly appreciated!

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about iphone