2012-10-23 88 views
0

我遞給(NSInteger) pageIndex和需要打印的網頁上NSInteger的爲const char *

void CGContextShowTextAtPoint ( CGContextRef c, CGFloat x, CGFloat y, const char *string, size_t length );

我怎麼得到const char *string,不要忘了字符串的長度

我傾向於結束與討厭的把戲整數轉換爲一個字符串,然後做cStringUsingEncoding:NSMacOSRomanStringEncoding但這不可能是最優雅的方式

爲完整性,這裏是代碼

const char *pageIndexString = [[NSString stringWithFormat:@"%d", pageIndex] cStringUsingEncoding:NSMacOSRomanStringEncoding]; 
CGContextShowTextAtPoint(CGContextRef, CGFloat x, CGFloat y, pageIndexString, strlen(pageIndexString)); 

回答

3

試試這個:

NSString *tmp = [NSString stringWithFormat:@"%ld", pageIndex]; 
const char *str = [tmp UTF8String]; 
size_t length = [tmp length]; 
+0

這基本上是我沒有...不優雅,骯髒和醜陋。爲了完整起見,我編輯了問題並粘貼了代碼。 – vanHoesel

+0

@ user1765406也許是。那有什麼問題?另外,爲什麼使用Mac Roman編碼? – 2012-10-23 19:04:20

+0

是的,我非常驚訝於我必須使用'MacRoman',但這是我多少了解[https://developer.apple.com/library/ios/#documentation/GraphicsImaging/概念/ drawingwithquartz2d/dq_text/dq_text.html%23](使用石英2D參考繪圖)。糾正我,如果我做錯了,如果我可以繪製UTF文本......那會更好 – vanHoesel