gpt4 book ai didi

objective-c - CGContextShowTextAtPoint 和西里尔文字

转载 作者:行者123 更新时间:2023-11-29 13:34:11 25 4
gpt4 key购买 nike

我在图像上绘制文本时遇到一点问题。当我尝试显示英文文本时,一切正常。问题是,我需要本地化文本。当文本为俄语时,会显示一些奇怪的符号。任何帮助将不胜感激。

我正在使用以下代码来绘制文本:

CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1);
char* text = (char *)[addText cStringUsingEncoding:NSUTF8StringEncoding];
CGContextSelectFont(context, "Impact", 20, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextShowTextAtPoint(context, 20, h+addHeightBelow, text, strlen(text));

它显示如下:

Ruined text

所以结果,你应该使用Core Text,代码是:

CTFontRef fontRef=CTFontCreateWithName((CFStringRef)@"Impact", 20.0f, NULL);
CGColorRef color=[UIColor blackColor].CGColor;
NSDictionary *attrDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
(id)fontRef, (id)kCTFontAttributeName,
color, (id)kCTForegroundColorAttributeName,
nil];
NSAttributedString *stringToDraw=[[NSAttributedString alloc] initWithString:addText attributes:attrDictionary];

CTLineRef line=CTLineCreateWithAttributedString((CFAttributedStringRef)stringToDraw);
CGContextSetTextPosition(context, 20, h+addHeightBelow);
CTLineDraw(line, context);

CFRelease(line);
CFRelease(fontRef);
[stringToDraw release];

最佳答案

使用Core Text用于在图像上绘制本地化文本。引用this sample

关于objective-c - CGContextShowTextAtPoint 和西里尔文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11081725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com