Iphone CGContextShowTextAtPointのための日本語の文字
質問
また、アプリを使用してい CGContextShowTextAtPoint
テキストが表示は画面になります。たいディスプレイも日本語の文字が CGContextShowTextAtPoint
その入力は、C文字列です。いち)が変更になりました日本語の文字にa Cん。これが不可能な場合には、たいのですが手動で印刷を日本語での文字が画面内のdrawRect法です。
よろしくお願いします。
解決
CoreText支援できます:
CTFontGetGlyphsForCharacters
(iOS3.2以降)地図をUnicode文字のグリフCTFontDrawGlyphs
(iOS4.2以降)のグリフへのCGContext.
何度でも利用することができる。 CGContextShowGlyphs
るべき仕事がしたいなあと考えている変換マUniCharsるグリフ.より詳しく話すつもりですが こちらの:
古代、iOS3.2回答
を使用する必要がありUIKitます。
チェック [NSString drawAtPoint:...]
を取得します。
この その質問 に便利です。
どうなるかはわからないかを考えると、CoreGraphicテキストものでは絶対に無駄にはなりません。
他のヒント
私はイェンスEgebladによってCGFontGetGlyphsForUnicharsの再実装を使用してこの作業を取得することができました: GlyphDrawing.mm の
アプリケーションバンドルからOTFファイルとして日本語フォントの最初の負荷ます:
// Load font file from otf file
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"HStdNW8" ofType:@"otf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef _cgFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
そして、あなたがグリフにあなたのunicharテキストを変換し、それらを描くことができます:
NSString *text = @"日本語"
CGContextSetFont(context, _cgFont);
CGContextSetFontSize(context, 12);
CGGlyph textGlyphs[[text length]];
unichar textChars[[text length]];
for(int i = 0; i < [text length]; i++) {
textChars[i] = [text characterAtIndex:i];
}
CMFontGetGlyphsForUnichars(_cgFont, textChars, textGlyphs, [text length]);
CGContextShowGlyphsAtPoint(context, xCoord, yCoord, textGlyphs, [text length]);
何が価値があるために、私はCoreGraphicsでうまく動作するように日本語の文字を取得しようと長い時間を費やし、そしてそれは私を残しところ好きではありませんでした。
最後に、私は、テキストを処理するためにUILabelsを使用してに切り替えました。私は必要なすべてのCoreGraphicsのようなものが変換&アニメーションのサポートを使用して複製することができ、最終的に結果のコードは非常に簡単でした。
それはあなたの状況に応じて適切ではないかもしれないが、それは検討する価値がある。
この多分あなたは%話題スターター%に役立ちます。偉大なアドバイスのためのリズムFistmanに感謝!
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(context, characterSpacing);
CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
CGContextSetTextMatrix (context, myTextTransform);
CGGlyph glyphs[self.text.length];
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[self.text cStringUsingEncoding:NSUnicodeStringEncoding], glyphs, self.text.length);
float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2;
CGContextShowGlyphsAtPoint(context, rect.origin.x, centeredY, (const CGGlyph *)glyphs, self.text.length);
CFRelease(fontRef);