0

I am using these functions to draw text over an image:

char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0.0, 0.1, 0.1, 1);
CGContextShowTextAtPoint(context, 5, 20, text, strlen(text));

It works fine, except when I have character such as "ü", "ö", "ä" etc etc with strlen - it will cause a crash. So I tried to get the length of a string by using this before this line

char* text = (char *)[text cStringUsingEncoding:NSASCIIStringEncoding];
by doing this:
int length = [text length];
and then replace strlen(text) with length.

it doesn't cause a crash, but no text is drawn. I think the culprit here is the characters. I have tried both

kCGEncodingMacRoman
kCGEncodingFontSpecific

but nothing helped. Can you me with this? Thanks.

UPDATED: @Martin R:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, 90.0f, 90.0f, 8, 4 * 90.0f, colorSpace,     kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, 90.0f, 90.0f), img.CGImage);

CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0.8, 0.1, 0.1, 1);
[text1 drawAtPoint:(CGPoint){5, 20} withFont:[UIFont fontWithName:@"Helvetica" size:10.0f]];
CGImageRef finalImage = CGBitmapContextCreateImage(context);

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

UIImage *image = [UIImage imageWithCGImage:finalImage];
CGImageRelease(finalImage);

return image;
Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • I assume that it is a typo or copy/paste error that you used `text` on both sides of `char* text = (char *)[text ...]` ? – Martin R Jul 02 '13 at 11:43

1 Answers1

1

CGContextShowTextAtPoint() interprets the given text according to the specified encoding parameter of CGContextSelectFont(), which is Mac Roman in your case. So you should convert the string using

NSString *string = @"äöü";
const char *text = [string cStringUsingEncoding:NSMacOSRomanStringEncoding];

Alternatively, you could use the drawAtPoint:withFont: method of NSString, which handles Unicode characters automatically:

NSString *string = @"äöü€";
[string drawAtPoint:CGPointMake(5, 20) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @Unheilig: Not with CGContextShowTextAtPoint(). For CGContextSelectFont() you can only choose kCGEncodingFontSpecific (which is quite useless) and kCGEncodingMacRoman (which does not contain Japanese characters, and has problems e.g. with the Euro/€ character). - The proper solution is to use drawAtPoint:withFont:. – Martin R Jul 02 '13 at 11:53
  • @Unheilig: See updated answer. Note however that the orientation is different, you might have to modify the transformation matrix. – Martin R Jul 02 '13 at 11:55
  • 1
    @Unheilig: 1) Yes, it works with all Unicode characters (if available in the font). - 2) When I tested the `drawAtPoint` solution, the text was upside down compared to the `CGContextShowTextAtPoint()` method. Perhaps you just try it first in your project. – Martin R Jul 02 '13 at 12:00
  • @Unheilig: I have tested the `drawAtPoint` method with `@"Krzyżanów"`, Japanese and even Emoji characters, without any problem. – Martin R Jul 03 '13 at 07:12