ios - 获取具有下降器的 UIFont 的所有字形
问题描述
有没有办法获得包含真正下降器的 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)")
解决方案
由于您的实际目标似乎是确定一个字符串是否包含一个带下降器的字符,您可以使用 Core Text 查看每个字形的边界矩形。如果边界矩形的原点为负数,则表示字形从基线下方开始。对于诸如y
but also之类的字符也是如此,
。
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,")
您可能希望根据需要添加额外的检查以仅查看字母。
推荐阅读
- c# - 模拟异步方法时单元测试失败,c#
- java - properties.store(FileOutputStream,comments) 在写作时转义了我的特殊字符
- java - 错误=216,此版本的 %1 与您正在运行的 Windows 版本不兼容
- python - 在 Python 中使用子文件夹的名称为每个子文件夹创建空文件
- python - pytest AttributeError:“函数”对象没有属性“get_marker”
- excel - VBA 清除 ActiveX 组合框列表
- javascript - how do i show a div on clicking another div and hide it on clicking that same div?
- r - How to assign day of year values starting from an arbitary date and take care of missing values?
- javascript - Focus event for button doesn't fire on iPad
- python - TensorFlow 中简单前馈 NN 的 GPU 训练的有效示例实现?也许与 tf.data?