Created
July 9, 2019 14:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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