iPhone - 使用CGContextShowTextAtPoint绘制特殊字符 [英] iPhone - Using CGContextShowTextAtPoint to draw special characters

查看:183
本文介绍了iPhone - 使用CGContextShowTextAtPoint绘制特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建UILabel的子类来调整字符间距。它工作得很好。
但是当我使用特殊字符作为西班牙语或日语,它的不写。只能正确写入英文字符。任何解决方案如何显示它们?

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]);
}


推荐答案

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

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

两个选择是Cocoa的文本绘图或CoreText。

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

这篇关于iPhone - 使用CGContextShowTextAtPoint绘制特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆