Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
retrieve width of space character and compute how many spaces are required to fill the gap
let spaceWidth = { () -> Double in
let font = self.font!
var spaceGlyph = CGGlyph()
var space: UniChar = numericCast((" " as Unicode.Scalar).value)
_ = CTFontGetGlyphsForCharacters(font, &space, &spaceGlyph, 1)
return CTFontGetAdvancesForGlyphs(font, .horizontal, &spaceGlyph, nil, 1)
// to be called from func layoutManager(_ layoutManager: NSLayoutManager, shouldSetLineFragmentRect lineFragmentRect: UnsafeMutablePointer<CGRect>, lineFragmentUsedRect: UnsafeMutablePointer<CGRect>, baselineOffset: UnsafeMutablePointer<CGFloat>, in textContainer: NSTextContainer, forGlyphRange glyphRange: NSRange)
let addSpaces = Int(Double(lineFragmentRect.pointee.maxX - lineFragmentUsedRect.pointee.maxX) / spaceWidth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.