0

我收到了一个(NSInteger) pageIndex并且需要在一个页面上打印它

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

1 回答 1

4

尝试这个:

NSString *tmp = [NSString stringWithFormat:@"%ld", pageIndex];
const char *str = [tmp UTF8String];
size_t length = [tmp length];
于 2012-10-23T18:14:14.470 回答