2012-10-23 87 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