代码之家  ›  专栏  ›  技术社区  ›  kiyoshi

用于日语字符的Iphone CGContextShowTextAtPoint

  •  8
  • kiyoshi  · 技术社区  · 17 年前

    我正在开发一个我正在使用的应用程序 CGContextShowTextAtPoint 在屏幕上显示文本。我也想显示日文字符,但是 CGContextShowTextAtPoint 将C字符串作为其输入。那么A)如何将日语字符转换为C字符串?如果不可能,B)如何手动将日语字符打印到屏幕上(在drawRect方法中)。

    提前谢谢。

    4 回复  |  直到 13 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

    CoreText可以帮助您:

    1. CTFontGetGlyphsForCharacters (iOS 3.2及以后版本)将Unicode字符映射到字形
    2. CTFontDrawGlyphs (iOS 4.2及以后版本)将图示符绘制到CGContext中。

    注意。 CGContextShowGlyphs here :

    古老的iOS 3.2版之前的答案

    您需要为此使用UIKit。

    退房 [NSString drawAtPoint:...] 开始吧。

    这 SO question 这也很有用。

    我不知道他们是怎么想的核心文字的东西,这是无用的。

        2
  •  3
  •   noodl_es    16 年前

    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]);
    
        3
  •  1
  •   Kris Jenkins    16 年前

    值得一提的是,我花了很长时间试图让日文字符在CoreGraphics中很好地工作,但不喜欢它留给我的地方。

    这可能不适合你的情况,但值得考虑。

        4
  •  0
  •   Valerii Pavlov    14 年前

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