方法 glyphWithName: 需要一个 glyphName,而不是字符。对于简单的拉丁字符,glyphName 与字符相同 - @"A"。据我所知,汉字没有名字,虽然平假名和片假名有。汉字实在太多了,许多字形都是同一个汉字的变体。
所以你必须对汉字使用不同的方法。这是一个对我有用的例子。
// Convert a single character to a bezier path
- (UIBezierPath *)bezierPathFromChar:(NSString *)aChar inFont:(CTFontRef)aFont {
// Buffers
unichar chars[1];
CGGlyph glyphs[1];
// Copy the character into a buffer
chars[0] = [aChar characterAtIndex:0];
// Encode the glyph for the single character into another buffer
CTFontGetGlyphsForCharacters(aFont, chars, glyphs, 1);
// Get the single glyph
CGGlyph aGlyph = glyphs[0];
// Find a reference to the Core Graphics path for the glyph
CGPathRef glyphPath = CTFontCreatePathForGlyph(aFont, aGlyph, NULL);
// Create a bezier path from the CG path
UIBezierPath *glyphBezierPath = [UIBezierPath bezierPath];
[glyphBezierPath moveToPoint:CGPointZero];
[glyphBezierPath appendPath:[UIBezierPath bezierPathWithCGPath:glyphPath]];
CGPathRelease(glyphPath);
return glyphBezierPath;
}
像这样使用:
NSString *theChar = @"男";
CTFontRef font = CTFontCreateWithName(CFSTR("HiraKakuProN-W6"), 114.0, NULL);
UIBezierPath *glyphBezierPath = [self bezierPathFromChar:theChar inFont:font];
编辑 - 定义可以本地化的字体的另一种方式:
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica-Bold", 114.0, NULL);