UTF8String产生字母符号¬£objective-c

时间:2011-03-06 03:13:16

标签: iphone objective-c utf-8

出于某种原因,当我尝试在我的iPhone应用程序中将货币符号打印到pdf文档中时,我可以打印而不是£。 (当iPhone设置为英国设置时使用英镑符号。)

这是我的代码:

text = [NSString stringWithFormat:@"%@", [Helper convertDoubleToCurrencyString:[item.Total floatValue]]]; 
                                            WriteOutTextInPDF(pdfContext, [text UTF8String], priceX, currentY, 16);

+(NSString*) convertDoubleToCurrencyString:(double) d{
    NSDecimalNumber *decimal = (NSDecimalNumber*)[NSDecimalNumber numberWithDouble:d];
    NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    return [currencyFormatter stringFromNumber:decimal];
}

void WriteOutTextInPDF(CGContextRef pdfContext, const char *text, int x, int y, int fontSize){
    CGContextSelectFont (pdfContext, "Helvetica", fontSize, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
    CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
    CGContextShowTextAtPoint (pdfContext, x, y, text, strlen(text));
}

我是如何得到符号而不是这种表示的?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

在UTF-8文本中,字符£由两个字节0xc2 0xa3表示。但在Mac Roman encoding中,这两个字节代表两个字符¬和£。

您可以使用[text cStringUsingEncoding:NSMacOSRomanStringEncoding]代替[text UTF8String]来使用CGGraphics期望的编码转换字符串。或者您可以尝试使用UIGraphicsPushContextUIKit string drawing functions使用UIKit绘制字符串,这可能支持UTF-8。