Macos CGFontGetGlyphWithGlyphName()出现问题
我一直在尝试使用CGFontGetGlyphWithGlyphName方法创建CGGlyph。它适用于所有字母,但不适用于数字Macos CGFontGetGlyphWithGlyphName()出现问题,macos,cocoa,core-graphics,Macos,Cocoa,Core Graphics,我一直在尝试使用CGFontGetGlyphWithGlyphName方法创建CGGlyph。它适用于所有字母,但不适用于数字 -(void)drawInContext:(CGContextRef)ctx{ CGContextSetAllowsAntialiasing(ctx, true); CGContextSetFont(ctx, font); CGContextSetFontSize(ctx, size); CGGlyph glyph = CGFontGet
-(void)drawInContext:(CGContextRef)ctx{
CGContextSetAllowsAntialiasing(ctx, true);
CGContextSetFont(ctx, font);
CGContextSetFontSize(ctx, size);
CGGlyph glyph = CGFontGetGlyphWithGlyphName(font, CFSTR("L"));
CGContextShowGlyphsAtPositions(ctx, &glyph, &(textbounds.origin), 1);
}
当我试图画数字时,它不起作用:
-(void)drawInContext:(CGContextRef)ctx{
CGContextSetAllowsAntialiasing(ctx, true);
CGContextSetFont(ctx, font);
CGContextSetFontSize(ctx, size);
CGGlyph glyph = CGFontGetGlyphWithGlyphName(font, CFSTR("3"));
CGContextShowGlyphsAtPositions(ctx, &glyph, &(textbounds.origin), 1);
}
“3”的字形可能被命名为“三”。这是我的测试:
import Foundation
import CoreGraphics
let font = CGFont("Helvetica" as CFString)!
for g in CGGlyph.min ... CGGlyph.max {
if let name = font.name(for: g) {
print("\(g) \(name)")
}
}
输出:
... many lines omitted ....
17 period
18 slash
19 zero
20 one
21 two
22 three
23 four
24 five
25 six
26 seven
... many more lines omitted ...
您最好使用核心文本CTFontGetGlyphsForCharacters函数。请注意,它使用CTFontRef,而不是CGFontRef。您可以使用CTFontCreateWithGraphicsFont将CGFontRef转换为CTFontRef