获取具有下行字母的 UIFont 的所有字形
Get all Glyphs of a UIFont that have a descender
有没有办法获取包含真正下行字母的 UIFont 的所有字形?似乎使用 CTLineGetTypographicBounds 并不准确,并且 returns 每行的下降值完全相同。我以为它会提供我需要的信息,但它没有。所以现在我想看看是否可以从包含真正下行字母的字形构建字符集,除非有其他方法。最终目标是能够查看一行文本是否低于基线。
let line = CTLineCreateWithAttributedString(NSAttributedString(string: s, attributes: attr))
//let's get the real descent test
var a : CGFloat = 0
var d : CGFloat = 0
var l : CGFloat = 0
let bounds = CTLineGetTypographicBounds(line, &a, &d, &l)
print("the descent is \(d)")
print("the ascent is \(a)")
print("the leading is \(l)")
由于您的实际目标似乎是确定字符串是否包含带有下行字符的字符,因此您可以使用 Core Text 查看每个字形的边界矩形。如果边界矩形的原点为负,这意味着字形从基线以下开始。对于 y
和 ,
.
这样的字符都是如此
func checkDescender(string: String) {
let uiFont = UIFont.systemFont(ofSize: 14) // Pick your font
let font = CTFontCreateWithName(uiFont.fontName as CFString, uiFont.pointSize, nil)
for ch in string.unicodeScalars {
let utf16codepoints = Array(ch.utf16)
var glyphs: [CGGlyph] = [0, 0]
let hasGlyph = CTFontGetGlyphsForCharacters(font, utf16codepoints, &glyphs, utf16codepoints.count)
if hasGlyph {
let rect = CTFontGetBoundingRectsForGlyphs(font, .default, glyphs, nil, 1)
// print("\(ch) has bounding box of \(rect)")
if rect.origin.y < 0 {
print("\(ch) goes below the baseline by \(-rect.origin.y)")
}
}
}
}
checkDescender(string: "Ymy,")
您可能希望根据需要添加额外的检查以仅查看字母。
有没有办法获取包含真正下行字母的 UIFont 的所有字形?似乎使用 CTLineGetTypographicBounds 并不准确,并且 returns 每行的下降值完全相同。我以为它会提供我需要的信息,但它没有。所以现在我想看看是否可以从包含真正下行字母的字形构建字符集,除非有其他方法。最终目标是能够查看一行文本是否低于基线。
let line = CTLineCreateWithAttributedString(NSAttributedString(string: s, attributes: attr))
//let's get the real descent test
var a : CGFloat = 0
var d : CGFloat = 0
var l : CGFloat = 0
let bounds = CTLineGetTypographicBounds(line, &a, &d, &l)
print("the descent is \(d)")
print("the ascent is \(a)")
print("the leading is \(l)")
由于您的实际目标似乎是确定字符串是否包含带有下行字符的字符,因此您可以使用 Core Text 查看每个字形的边界矩形。如果边界矩形的原点为负,这意味着字形从基线以下开始。对于 y
和 ,
.
func checkDescender(string: String) {
let uiFont = UIFont.systemFont(ofSize: 14) // Pick your font
let font = CTFontCreateWithName(uiFont.fontName as CFString, uiFont.pointSize, nil)
for ch in string.unicodeScalars {
let utf16codepoints = Array(ch.utf16)
var glyphs: [CGGlyph] = [0, 0]
let hasGlyph = CTFontGetGlyphsForCharacters(font, utf16codepoints, &glyphs, utf16codepoints.count)
if hasGlyph {
let rect = CTFontGetBoundingRectsForGlyphs(font, .default, glyphs, nil, 1)
// print("\(ch) has bounding box of \(rect)")
if rect.origin.y < 0 {
print("\(ch) goes below the baseline by \(-rect.origin.y)")
}
}
}
}
checkDescender(string: "Ymy,")
您可能希望根据需要添加额外的检查以仅查看字母。