Iphone CGContextShowTextAtPoint for Japanese characters

烈酒焚心 提交于 2019-12-17 16:36:00

问题


I am working on an app where I am using CGContextShowTextAtPoint to display text to the screen. I want to also display Japanese characters, but CGContextShowTextAtPoint takes as its input a C string. So either A) How do I change Japanese characters into a C string? If this is not possible, B) How can I manually print Japanese characters to the screen (within the drawRect method).

Thanks in advance.


回答1:


CoreText can help you:

  1. CTFontGetGlyphsForCharacters (iOS 3.2 onwards) maps Unicode characters to glyphs
  2. CTFontDrawGlyphs (iOS 4.2 onwards) draws the glyphs into a CGContext.

NB. CGContextShowGlyphs should work, but I never found a way to convert my UniChars to glyphs. More on that here:

Ancient, pre iOS 3.2 answer

you need to use UIKit for this.

Check out [NSString drawAtPoint:...] to get started.

This SO question is useful, too.

I don't know what they were thinking with the CoreGraphic text stuff, it's useless.




回答2:


I was able to get this working by using a reimplementation of CGFontGetGlyphsForUnichars by Jens Egeblad: GlyphDrawing.mm

First load in a Japanese font as an otf file from the app bundle:

// 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);

Then you can convert your unichar text to glyphs and draw them:

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



回答3:


For what it's worth, I spent a long time trying to get Japanese characters to work well in CoreGraphics, and didn't like where it left me.

In the end I switched to using UILabels to handle the text. All the CoreGraphics-like stuff I needed could be replicated using the transform & animation support, and in the end the resulting code was much simpler.

It may not be appropriate for your situation, but it's worth considering.




回答4:


This maybe helps you %topic starter%. Thanks to Rhythmic Fistman for great advice!

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


来源:https://stackoverflow.com/questions/1237565/iphone-cgcontextshowtextatpoint-for-japanese-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!