2011-08-25 59 views
2

我正在爲UILabel創建子類來調整字符間距。它運作良好。 但是,當我使用西班牙語或日語的特殊字符時,它不會寫入。只有英文字符可以正確寫入。有關如何顯示它們的任何解決方案?iPhone - 使用CGContextShowTextAtPoint繪製特殊字符

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

根據URL [CGContextShowTextAtPoint] [1]中的答案,這是不可能的。 [1]:http://stackoverflow.com/questions/1564577/converting-a-nsstring-to-a-cstring-for-use-with-cgcontextshowtextatpoint – Satyam

回答

1

CGContextShowTextAtPoint不直接支持這些語言 - MacRoman編碼可能是一個提示。 CG只有基本的文字佈局/繪畫。

兩種選擇將是可可的文本繪圖或CoreText。

1

比從未更好的遲到;

使用CoreText以drawFonts代替(特殊字符和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和更高版本和Mac OS X都支持核心文本,先進的低層次的技術佈置文本和移交字體。核心文本設計以便高性能和易用性,並允許您將Unicode文本直接繪製到圖形上下文中。如果您正在編寫需要精確控制文本顯示方式的應用程序,請參閱Core Text Programming Guide。「