Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active October 21, 2023 16:57
Show Gist options
  • Save krzyzanowskim/7adc5ee66be68df2f76b9752476aadfb to your computer and use it in GitHub Desktop.
Save krzyzanowskim/7adc5ee66be68df2f76b9752476aadfb to your computer and use it in GitHub Desktop.
NSTextContainer.lineFragmentPadding does not affect end of the fragment usageBoundsForTextContainer rectangle

I noticed that NSTextContainer.lineFragmentPadding value does not affect the NSTextLayoutManager.usageBoundsForTextContainer rectangle on both sizes.

Documentation of lineFragmentPadding says: "The padding appears at the beginning and end of the line fragment rectangles.", however the value of the usageBoundsForTextContainer does not resize appropriately.

Let's see how a different value of lineFragmentPadding affect the text container rectangle:

for lineFragmentPadding = 5 usageBoundsForTextContainer = (x: 5.0, y: 0.0, width: 66.73828125, height: 14.0)

for lineFragmentPadding = 0 usageBoundsForTextContainer = (x: 0.0, y: 0.0, width: 66.73828125, height: 14.0)

as the "x" value updated as expected, the width did not change. The expected change would be to add (or substract) the value of lineFragmentPadding from the container width

the full sample code:

        // Storage
        let textContentManager = NSTextContentStorage()

        // Layout
        let textLayoutManager = NSTextLayoutManager()
        textContentManager.addTextLayoutManager(textLayoutManager)
        textContentManager.primaryTextLayoutManager = textLayoutManager

        let textContainer = NSTextContainer()
        textLayoutManager.textContainer = textContainer
        
        textContentManager.attributedString = NSAttributedString(string: "01234567890123456789")

        textContainer.lineFragmentPadding = 5
        textLayoutManager.ensureLayout(for: textContentManager.documentRange)
        print(textLayoutManager.usageBoundsForTextContainer) // (5.0, 0.0, 66.73828125, 14.0)

        textContainer.lineFragmentPadding = 0
        textLayoutManager.ensureLayout(for: textContentManager.documentRange)
        print(textLayoutManager.usageBoundsForTextContainer) // (0.0, 0.0, 66.73828125, 14.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment