Ios 使用CGContextShowTextAtPoint显示文本会绘制奇怪的文本

Ios 使用CGContextShowTextAtPoint显示文本会绘制奇怪的文本,ios,xcode,text,sdk,cgcontext,Ios,Xcode,Text,Sdk,Cgcontext,我只是尝试在UIImage中覆盖一个文本(使用CGContext),但它没有显示我指定的文本。在这段代码中,我给出的文本参数是“Hello”,但它显示的是“eääc”。我不知道发生了什么,我想这些角色被替换了,但我不知道如何解决。 这是我的代码: UIGraphicsBeginImageContextWithOptions(image.size, YES, 0); CGContextRef c = UIGraphicsGetCurrentContext(); [image drawInRect:

我只是尝试在UIImage中覆盖一个文本(使用CGContext),但它没有显示我指定的文本。在这段代码中,我给出的文本参数是“Hello”,但它显示的是“eääc”。我不知道发生了什么,我想这些角色被替换了,但我不知道如何解决。 这是我的代码:

UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextSetTextMatrix(c, CGAffineTransformMake(1.0, 0, 0, -1.0, 0, 0));
CGContextSelectFont(c, "ArialMT", 50, kCGEncodingFontSpecific);
CGContextSetRGBStrokeColor(c, 255, 0, 0, 1);
CGContextSetRGBFillColor(c, 255, 0, 0, 1);
CGContextSetCharacterSpacing(c, 2);
CGContextShowTextAtPoint(c,100,100, "Hello", 5);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我使用的是西班牙语键盘(我不知道这是否重要),iPad(我测试应用程序的地方)是加泰罗尼亚语。

CGContextShowTextAtPoint()
根据指定的编码参数
CGContextSelectFont
解释给定的文本。您选择了
kCGEncodingFontSpecific
,这是字体内置编码(不管是什么)

唯一的其他选择是
kgencodingmacroman

CGContextSelectFont(c, "ArialMT", 50, kCGEncodingMacRoman);
然后,只要只使用ASCII字符集中的字符,文本就应该正确显示

对于非ASCII字符,必须将字符串转换为MacRoman编码,例如,请参见示例

请注意,
CGContextShowTextAtPoint
无法显示通用Unicode字符串,甚至欧元(€)字符也有问题。
NSString
drawAtPoint:withFont:
方法没有这些限制