如何使用CGContextSelectFont根据当前语言选择编码?

发布于 2024-11-24 01:02:22 字数 569 浏览 5 评论 0原文

使用 CGContextSelectFont 绘制位图,但使用某种编码。当使用不同的语言时,这将不起作用。根据当前语言选择编码的推荐方式是什么?请注意,本地化工作正常。

CGContextDrawImage(context, rect, image.CGImage);

NSString *temp = [[NSString alloc] initWithFormat:@"%@%@",[Localization string:@"WaitingForDownloadFor"],title];
const char *text = [temp UTF8String];
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);  //*** line in question
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextShowTextAtPoint(context, 10, h/2, text, strlen(text));

Using CGContextSelectFont to draw to a bitmap, but using a certain encoding. When using a different language, this will not work. What is the recommended way of choosing the encoding based on current language? Please note that the localization is working fine.

CGContextDrawImage(context, rect, image.CGImage);

NSString *temp = [[NSString alloc] initWithFormat:@"%@%@",[Localization string:@"WaitingForDownloadFor"],title];
const char *text = [temp UTF8String];
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);  //*** line in question
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextShowTextAtPoint(context, 10, h/2, text, strlen(text));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-12-01 01:02:22

这是回答问题的代码,感谢您的指点......Localization 类是一个自定义类。

NSString *temp = [[NSString alloc] initWithFormat:@"%@%@",[Localization string:@"Downloading"],title];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, rect, image.CGImage);    
UIGraphicsPushContext(context);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
UIFont* font = [UIFont fontWithName:@"Arial" size:18.0];
// need to do this or Mirrored Backwards
CGContextTranslateCTM(context, 0, h);
CGContextScaleCTM(context, 1.0, -1.0);
[temp drawAtPoint:CGPointMake(10.0,h/2) withFont:font];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *image2 = [[UIImage imageWithCGImage:imageRef] retain];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);    
UIGraphicsPopContext();

Here is the code that answers the question, thanks for the pointers....The Localization class is a custom class.

NSString *temp = [[NSString alloc] initWithFormat:@"%@%@",[Localization string:@"Downloading"],title];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, rect, image.CGImage);    
UIGraphicsPushContext(context);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
UIFont* font = [UIFont fontWithName:@"Arial" size:18.0];
// need to do this or Mirrored Backwards
CGContextTranslateCTM(context, 0, h);
CGContextScaleCTM(context, 1.0, -1.0);
[temp drawAtPoint:CGPointMake(10.0,h/2) withFont:font];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *image2 = [[UIImage imageWithCGImage:imageRef] retain];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);    
UIGraphicsPopContext();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文