Skip to content

Instantly share code, notes, and snippets.

@kambala-decapitator
Created July 9, 2019 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kambala-decapitator/e88608f4919c2a792d6c0f451cbaa679 to your computer and use it in GitHub Desktop.
Save kambala-decapitator/e88608f4919c2a792d6c0f451cbaa679 to your computer and use it in GitHub Desktop.
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