2011-03-06 49 views
1

由於某些原因,當我嘗試在我的iPhone應用程序中將貨幣符號打印到PDF文檔中時,我得到的是印刷而不是£。 (當iPhone設置爲英國設置時,使用英鎊符號)。UTF8String產生字面磅符號¬£objective-c

這是我的代碼:

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)); 
} 

任何想法如何,我得到了符號,不要使用這種表示?

任何幫助將不勝感激。

回答

2

在UTF-8文本中,字符£由兩個字節0xc2 0xa3表示。但在Mac Roman encoding中,這兩個字節表示兩個字符¬和£。

您可以使用[text cStringUsingEncoding:NSMacOSRomanStringEncoding]而不是[text UTF8String]來使用CGGraphics預期的編碼轉換字符串。或者,您可以嘗試使用UIGraphicsPushContextUIKit string drawing functions來繪製UIKit字符串,該字符串可能支持UTF-8。

+0

謝謝。這解釋了這一點。我注意到這兩個字節,但不知道寫出它們的Mac羅馬錶示。 – user646598 2011-03-06 08:45:12