iPhone - 使用CGContextShowTextAtPoint绘制特殊字符

发布于 2024-12-01 09:15:44 字数 1384 浏览 1 评论 0原文

我正在为 UILabel 创建子类来调整字符间距。效果很好。 但是当我在西班牙语或日语中使用特殊字符时,它就不是写作。只有英文字符才能正确书写。关于如何显示它们有什么解决方案吗?

- (void) drawRect:(CGRect)rect 
{

    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSUTF8StringEncoding], self.font.pointSize, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing(context, -1);
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
    CGContextSetTextMatrix (context, myTextTransform);

    // draw 1 but invisbly to get the string length.
    const char* str = [self.text UTF8String];

    CGPoint p = CGContextGetTextPosition(context);
    float centeredY = (self.font.pointSize + (self.frame.size.height - self.font.pointSize)/2)-2;
    CGContextShowTextAtPoint(context, 0, centeredY, str, [self.text length]);
    CGPoint v = CGContextGetTextPosition(context);

    float centeredX = 0;
    if (self.centered) {
        float width = v.x - p.x;
        centeredX = (self.frame.size.width- width)/2;
    }
    // calculate width and draw second one.
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
    CGContextShowTextAtPoint(context, centeredX, centeredY, str, [self.text length]);
}

I am creating sub class for UILabel to adjust the character spacing. It works well.
But when I use special characters as in Spanish or Japanese language, its not writing. Only english characters are written properly. Any solution on how to display them?

- (void) drawRect:(CGRect)rect 
{

    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSUTF8StringEncoding], self.font.pointSize, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing(context, -1);
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
    CGContextSetTextMatrix (context, myTextTransform);

    // draw 1 but invisbly to get the string length.
    const char* str = [self.text UTF8String];

    CGPoint p = CGContextGetTextPosition(context);
    float centeredY = (self.font.pointSize + (self.frame.size.height - self.font.pointSize)/2)-2;
    CGContextShowTextAtPoint(context, 0, centeredY, str, [self.text length]);
    CGPoint v = CGContextGetTextPosition(context);

    float centeredX = 0;
    if (self.centered) {
        float width = v.x - p.x;
        centeredX = (self.frame.size.width- width)/2;
    }
    // calculate width and draw second one.
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
    CGContextShowTextAtPoint(context, centeredX, centeredY, str, [self.text length]);
}

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

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

发布评论

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

评论(2

绮烟 2024-12-08 09:15:44

CGContextShowTextAtPoint 不直接支持这些语言——MacRoman 编码可能是一个提示。 CG只有基本的文字布局/绘图。

两个替代方案是 Cocoa 的文本绘制或 CoreText。

CGContextShowTextAtPoint does not directly support those languages -- the MacRoman encoding may have been a hint. CG has only basic text layout/drawing.

two alternatives would be Cocoa's text drawing, or CoreText.

窝囊感情。 2024-12-08 09:15:44

Better late than never;

Use CoreText to drawFonts instead (Special characters and Unicode)
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101
"iOS 3.2 and later and Mac OS X both support Core Text, an advanced low-level technology for laying out text and handing fonts. Core Text is designed for high performance and ease of use and allows you to draw Unicode text directly to a graphics context. If you are writing an application that needs precise control over how text is displayed, see Core Text Programming Guide."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文