CGFontGetGlyphWithGlyphName() 的问题
Trouble with CGFontGetGlyphWithGlyphName()
我一直在尝试使用 CGFontGetGlyphWithGlyphName() 方法创建 CGGlyph。它适用于所有字母,但不适用于数字。
-(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);
}
Windows shows L letter
当我尝试绘制数字时它不起作用:
-(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);
}
Window shows empty rect
“3”的字形可能命名为 three
(拼写)。这是我的测试:
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 ...
您最好使用 Core Text CTFontGetGlyphsForCharacters
功能。请注意,它使用 CTFontRef
,而不是 CGFontRef
。您可以使用 CTFontCreateWithGraphicsFont
.
将 CGFontRef
转换为 CTFontRef
我一直在尝试使用 CGFontGetGlyphWithGlyphName() 方法创建 CGGlyph。它适用于所有字母,但不适用于数字。
-(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);
}
Windows shows L letter
当我尝试绘制数字时它不起作用:
-(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);
}
Window shows empty rect
“3”的字形可能命名为 three
(拼写)。这是我的测试:
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 ...
您最好使用 Core Text CTFontGetGlyphsForCharacters
功能。请注意,它使用 CTFontRef
,而不是 CGFontRef
。您可以使用 CTFontCreateWithGraphicsFont
.
CGFontRef
转换为 CTFontRef