views:

864

answers:

1

Hi,

I am drawing a String using CGContextShowTextAtPoint. Thus I need to convert my NSString I want to draw into c Strings. Unfortunately special symbols like the euro currency symbol are not correctly shown.

CGContextSelectFont(currentContext, "TrebuchetMS", 15, kCGEncodingMacRoman);

CGContextShowTextAtPoint(currentContext, 0, 0, [myString cStringUsingEncoding:[NSString defaultCStringEncoding]], [myString length]);

I tried it with the kCGEncodingFontSpecific encoding in the CGContextSelectFont function but that didn't work either.

For performance reasons I need to use the CG Function, not the drawInRect functions provieded by NSString.

Maybe you can help me!

Best, heinrich

PS: I know this is an often issued topic, but I cannot figure out why I can't get it working ...

+1  A: 

The "special symbols" you are referring to are supported by the Unicode encoding of NSString, but not the MacRoman encoding used by your Core Graphics font drawing routines (the only two encodings you can set using CGContextSelectFont() are kCGEncodingMacRoman and kCGEncodingFontSpecific). That's the disadvantage of the CGContextShowTextAtPoint() route for drawing text. Because of this, I use NSString's -drawAtPoint: method whenever I need to manually draw text within a Core Graphics context.

As far as performance goes, in a comment on an earlier answer, Kyle had benchmarked drawAtPoint: as drawing 75 times per second versus CGContextShowTextAtPoint() drawing 99 times per second. That's not a tremendous difference in draw speed, so you're not gaining a lot by going this way. In my experience, -drawAtPoint: has been more than fast enough for my applications.

EDIT (10/14/2009): As pointed out by Sixten Otto, I misread my own code. UTF-8 does support the full character range, the font's MacRoman does not.

Brad Larson
Any Unicode character can be expressed in UTF-8.
Sixten Otto
So wouldn't the problem be solved if I converted my NSStrings to c strings using unicode encoding?
Heinrich
Depends on whether the CoreGraphics text drawing a) is Unicode-aware, and b) will pull in missing glyphs from other fonts. TrebuchetMS may not have glyphs for all of the characters you're trying to draw. Again, you'd be much better off with the NSString methods.
Sixten Otto
I corrected my answer in regards to the UTF-8 encoding. I had been mistaken there. It's the MacRoman encoding for the font that doesn't support much of the Unicode character set.
Brad Larson
I'll give the a drawInRect methods a try. Not sure whether they suffice for me.
Heinrich