我正在为 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/