获取除字母或数字以外的字符的 CGGlyphs 的边界矩形

Get bounding rectangle for CGGlyphs of Characters other then letters or numbers

提问人:Rasto 提问时间:11/21/2014 最后编辑:Vadim KotovRasto 更新时间:4/10/2018 访问量:1543

问:

我正在寻找一种方法来记录字符的字形下降,如图所示:

enter image description here

该方法需要适用于任何给定字符(或至少所有常见的 Unicode 字符)。

这是我的实际方法(在 Swift 中,受到这个问题这个问题的启发):

let char = "a"
let ctFont = CTFontCreateWithNameAndOptions("HelveticaNeue", 12, nil, nil)
var ctGlyph = CTFontGetGlyphWithName(ctFont, char)
let boundingBox = withUnsafePointer(&ctGlyph) { pointer -> CGRect in
    return CTFontGetBoundingRectsForGlyphs(ctFont, CTFontOrientation.OrientationDefault, pointer, nil, 1)
}

那么我需要的下降就等于 . 这种方法适用于字母、数字和数字字符(有关图形表示,请参阅此答案)。-boundingBox.origin.y

问题是,对于字母或数字以外的所有内容(例如:),我得到相同的边界矩形:。这显然是不正确的。.,#')*{x:0.612, y:0.012, w:4.908, h:8.532}

如何直接获取所有字符的下降的绑定矩形?

iOS Swift 字符串 core-text

评论

0赞 Rasto 11/21/2014
代码示例在 Swift 中非常受欢迎。谢谢
0赞 David Rönnqvist 11/21/2014
检查文档中提到的 for 的返回参数:“如果无法识别字形名称,则返回字形 [返回]”CTFontGetGlyphWithName().notdef.notdef
0赞 Rasto 11/21/2014
@DavidRönnqvist 你是对的,这就是错误。把它作为一个答案,我会接受它。

答: 暂无答案