使用CGContextShowTextAtPoint打印时使用¬前符号打印的Unicode符号 [英] unicode symbols printing with ¬ before ° sign when printed using CGContextShowTextAtPoint

查看:147
本文介绍了使用CGContextShowTextAtPoint打印时使用¬前符号打印的Unicode符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CGContextShowTextAtPoint调用在视图上打印°或μ。该行代码如图所示。

  int number = 100; 
NSString * theText2 = [NSString stringWithFormat:@%d°,number];
CGContextShowTextAtPoint(context,10,5,[theText2 cStringUsingEncoding:NSUTF8StringEncoding],[theText2 length]);

这个输出结果如下:

100-



如果我增加打印字符串的长度,它会显示以下内容:

100 ¬°



预期产出应该是:

100°

这肯定需要打印unicode字符。

解决方案

对于 CGContextShowTextAtPoint 必须将字符串转换为在 CGContextSelectFont 中指定的编码。很可能这是Mac罗马编码。并且 CGContextShowTextAtPoint 的最后一个参数是转换后的字符串的长度,而不是 NSString $ b $ pre code $ CGContextSelectFont(context,Helvetica,10.0,kCGEncodingMacRoman);

int number = 100;
NSString * theText2 = [NSString stringWithFormat:@%d°μ,number];
const char * s = [theText2 cStringUsingEncoding:NSMacOSRomanStringEncoding];
CGContextShowTextAtPoint(context,10,5,s,strlen(s));

请注意,只有使用Mac Roman字符集中的字符时,此功能才有效。
(由于Mac Roman编码中的一些历史性变化,它也不适用于欧元字符。)



对于一般的Unicode字符串, NSString 方法 drawAtPoint:withFont:可能更适合。


I am printing a ° or a µ on my view using the CGContextShowTextAtPoint call. the line of code is as shown.

int number = 100;
NSString *theText2 = [NSString stringWithFormat:@"%d°", number];
CGContextShowTextAtPoint(context, 10, 5, [theText2 cStringUsingEncoding:NSUTF8StringEncoding], [theText2 length]);

The output of this comes out as

"100¬"

If I increase the length of the string printed it shows the following

"100¬°"

The expected output should be:

"100°"

This surely has to do something with printing unicode characters.

解决方案

For CGContextShowTextAtPoint you have to convert the string to the encoding that was specified in CGContextSelectFont. Most probably this is the "Mac Roman" encoding. And the last argument of CGContextShowTextAtPoint is the length of the converted string, not the length of the NSString:

CGContextSelectFont(context, "Helvetica", 10.0, kCGEncodingMacRoman);

int number = 100;
NSString *theText2 = [NSString stringWithFormat:@"%d°µ", number];
const char *s = [theText2 cStringUsingEncoding:NSMacOSRomanStringEncoding];
CGContextShowTextAtPoint(context, 10, 5, s, strlen(s));

Note that this works only as long as you use characters from the "Mac Roman" character set. (And it does also not work for the Euro character due to some historical changes in the Mac Roman encoding.)

For general Unicode strings the NSString method drawAtPoint:withFont: might be better suited.

这篇关于使用CGContextShowTextAtPoint打印时使用¬前符号打印的Unicode符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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