일본 문자를위한 iPhone CGContexTshowTextAtPoint
문제
사용중인 앱에서 작업 중입니다. CGContextShowTextAtPoint
화면에 텍스트를 표시합니다. 일본어 문자도 표시하고 싶지만 CGContextShowTextAtPoint
입력으로 C 문자열을 사용합니다. 그래서 a) 일본어 문자를 C 문자열로 어떻게 변경합니까? 이것이 불가능한 경우, b) 일본어 문자를 화면에 수동으로 인쇄 할 수있는 방법 (DrawRect 메소드 내).
미리 감사드립니다.
해결책
Coretext가 도움이 될 수 있습니다.
CTFontGetGlyphsForCharacters
(iOS 3.2 이후) 유니 코드 문자를 글리프에 매핑합니다.CTFontDrawGlyphs
(iOS 4.2 이후) Glyphs를 CGContext로 끌어냅니다.
NB. CGContextShowGlyphs
작동해야하지만 Unichars를 글리프로 변환하는 방법을 찾지 못했습니다. 그것에 대해 더 여기:
고대, iOS Pre 3.2 답변
이를 위해 Uikit을 사용해야합니다.
체크 아웃 [NSString drawAtPoint:...]
시작하려면.
이것 그래서 질문 유용합니다.
나는 그들이 Coregraphic 텍스트로 무엇을 생각하고 있는지 모르겠다. 그것은 쓸모가 없다.
다른 팁
Jens Egeblad의 CGFontgetGlyphsforUnichars를 다시 구현 하여이 작업을 수행 할 수있었습니다. 글리프 드래그 .m
앱 번들의 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);