In order to positioning / scaling font glyphs in a custom UIView I need to know some glyph metrics, such as: - ascent (height of the glyph from the base line, such as the part of "g" that stands over the base line) - descent (depth of the glyph from the base line, such as the part of "g" that stands under the base line) - width - kerning - italic correction (the part of the glyph that exceeds its width in italic)
I tried subclassing NSLayoutManager
and read those information from drawGlyphs
:
override func drawGlyphs(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
enumerateLineFragments(forGlyphRange: glyphsToShow) {
(rect, usedRect, textContainer, glyphRange, stop) in
for i in glyphsToShow.location ..< NSMaxRange(glyphsToShow) {
if let textContainer = self.textContainer(forGlyphAt: glyphsToShow.location, effectiveRange: nil) {
var glyphRect = self.boundingRect(forGlyphRange: NSMakeRange(i, 1), in:textContainer)
glyphRect.origin.x += origin.x;
glyphRect.origin.y += origin.y;
/// NOW I HAVE AT LEAST THE BOUNDING BOX
}
}
}
}
but glyphRect
has the same exact width/height for every glyph, so it carries the max (height+depth) vertical space and max width for the whole font, which isn't what I need (I need those information for every glyph: I is taller than i and j has depth while E hasn't).
Is it possible to collect this information via TextKit? Are the other font metrics (kerning, italic correction) available?
Thank you for your help, Luca.