I've been working on something like this.
Everything was working fine until I've found out a string input that causes an issue
The input string is "This is a long titlessss \n Withssssssss"
Basically, it seems like the "long" word fits the space but for some reason, it is being drawn in the next line.
When I set a new attributed text, I'm always updating the textStorage in this way
textStorage.setAttributedString(attributedText)
My UILabel's subclass calls
layoutManager.enumerateLineFragments(forGlyphRange: range) { (_, usedRect, _, glyphRange, _) in
inside drawText(in rect:)
Here glyphRange returns location 0 and length 15
for the first LineFragments ( so until long ) and the rect size is what you see drawn in the picture because it's the one I use to create the bezier path.
These are the settings for all the object of TextKit
let textContainer = NSTextContainer()
textContainer.heightTracksTextView = true
textContainer.lineFragmentPadding = 0
let layoutManager = NSLayoutManager()
layoutManager.usesFontLeading = false
layoutManager.allowsNonContiguousLayout = true
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .clear
lineBreakMode = .byCharWrapping
textContainer.lineBreakMode = lineBreakMode
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
isUserInteractionEnabled = true
I've tried everything. Some delegate methods, moving the logic all around.. I can't understand why that string is being drawn in the next line.