出于某种原因,当我尝试在我的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));
}
我是如何得到符号而不是这种表示的?
非常感谢任何帮助。
答案 0 :(得分:2)
在UTF-8文本中,字符£由两个字节0xc2 0xa3
表示。但在Mac Roman encoding中,这两个字节代表两个字符¬和£。
您可以使用[text cStringUsingEncoding:NSMacOSRomanStringEncoding]
代替[text UTF8String]
来使用CGGraphics期望的编码转换字符串。或者您可以尝试使用UIGraphicsPushContext
和UIKit string drawing functions使用UIKit绘制字符串,这可能支持UTF-8。