将NSString转换为cString以与CGContextShowTextAtPoint一起使用 [英] Converting a NSString to a cString for use with CGContextShowTextAtPoint

查看:117
本文介绍了将NSString转换为cString以与CGContextShowTextAtPoint一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CGContextShowTextAtPoint绘制字符串。因此,我需要将要绘制的NSString转换为c字符串。不幸的是,不能正确显示诸如欧元符号之类的特殊符号。

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

我在CGContextSelectFont函数中使用kCGEncodingFontSpecific编码进行了尝试,但这也不起作用。

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

出于性能原因,我需要使用CG函数,而不要使用NSString提供的drawInRect函数。

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

也许可以

PS:我知道这是一个经常发布的话题,但是我不知道为什么我无法使它正常工作...

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

推荐答案

NSString的Unicode编码支持您所指的特殊符号,但Core Graphics字体图形使用的MacRoman编码不支持例程(使用CGContextSelectFont()可以设置的唯一两种编码是kCGEncodingMacRoman和kCGEncodingFontSpecific)。这就是CGContextShowTextAtPoint()路由用于绘制文本的缺点。因此,每当需要在Core Graphics上下文中手动绘制文本时,我都会使用NSString的-drawAtPoint:方法。

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.

就性能而言,在< a href = https://stackoverflow.com/questions/844829/creating-a-custom-text-drawing-view/845433#845433>一个较早的答案,凯尔(Kyle)对drawAtPoint进行了基准测试:绘制了75次每秒与CGContextShowTextAtPoint()每秒绘制99次。绘制速度并没有太大的差别,因此通过这种方式并不会获得太大收益。以我的经验,-drawAtPoint:对于我的应用程序来说已经足够快了。

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.

编辑(10/14/2009):正如Sixten Otto指出的,我误读了我自己的代码。 UTF-8确实支持整个字符范围,而字体的MacRoman不支持。

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.

这篇关于将NSString转换为cString以与CGContextShowTextAtPoint一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆