gpt4 book ai didi

objective-c - 使用 CGContextShowTextAtPoint 打印时,在 ° 符号之前使用 ¬ 打印 unicode 符号

转载 作者:行者123 更新时间:2023-11-29 13:23:38 24 4
gpt4 key购买 nike

我正在使用 CGContextShowTextAtPoint 调用在我的 View 上打印 ° 或 µ。代码行如图所示。

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 Roman”编码。 CGContextShowTextAtPoint 的最后一个参数是转换后的字符串的长度,而不是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));

请注意,这仅在您使用“Mac Roman”字符集中的字符时有效。(由于 Mac Roman 编码的一些历史变化,它也不适用于欧元字符。)

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

关于objective-c - 使用 CGContextShowTextAtPoint 打印时,在 ° 符号之前使用 ¬ 打印 unicode 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13738158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com