iPhone - 使用CGContextShowTextAtPoint绘制特殊字符

标签 iphone objective-c cocoa-touch uilabel

我正在为 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]);
}

最佳答案

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

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

关于iPhone - 使用CGContextShowTextAtPoint绘制特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7190005/

相关文章:

ios - SKPhysicsJointPin 引脚位置

iphone - 如何确定背景区域何时被点击?

c++ - 正确使用@autoreleasepool

ios - searchBarCancelButtonClicked popToRootViewControllerAnimated

javascript - 将包含 NSDictionary 对象的 NSArray 传递给 Javascript 函数

ios - 当约束到包含多行 UILabel 的 UIStackview 时,UITableViewCell 高度不是动态的

iphone - 协同设计 "The operation was cancelled by the user"

iphone - 内存管理困境, Objective-C

iphone - 如何在项目中实现UIPageController

iphone - 以编程方式更改 UIKit 对象 anchor ?