Converting a NSString to a cString for use with CGContextShowTextAtPoint

≡放荡痞女 提交于 2019-12-21 12:42:17

问题


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!

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


回答1:


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.



来源:https://stackoverflow.com/questions/1564577/converting-a-nsstring-to-a-cstring-for-use-with-cgcontextshowtextatpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!