获取具有下行器的所有UIFont字形

问题描述 投票:1回答:1

有没有办法获得包含真正下降器的UIFont的所有字形?似乎使用CTLineGetTypographicBounds不准确,并返回每行的完全相同的下降值。我认为它会提供我需要的信息,但事实并非如此。所以现在我想看看我是否可以从包含真正下行的字形构建字符集,除非有另一种方法。最终目标是能够查看一行文字是否低于基线。

  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)")
ios swift core-text uifont
1个回答
1
投票

由于您的实际目标似乎是确定字符串是否包含带有下行符的字符,因此您可以使用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,")

您可能希望添加其他检查,仅根据您的需要查看字母。

© www.soinside.com 2019 - 2024. All rights reserved.