What is the recommended way to render a divider line in a NSTextView?

I would like to render a divider line in a NSTextView (like the <div> HTML item) on macOS 10.13 or later. I use Objective-C.

I've searched for solutions and it looks like the recommended solution is to use a NSTextAttachmentCell. There was supposedly an example provided on the cocoa-dev mailing list but the archives for this useful mailing list are gone.

The issue I'm observing with the NSTextAttachmentCell solution is that it is working almost well with a simple NSTextView example but when used in a more complex application, the rendering of the divider is still fine but there's a huge vertical gap between the divider and the text below the divider.

The online documentation for the NSTextAttachmentCell class is not really helpful (and contains typos: "With the exceptions of cellBaselineOffset, attachment, and attachment, all of these methods are implemented by the NSCell class.For general information on text attachments, " -> "attachment" twice, no space between "class." and "For general").

Here's the simple code I'm currently testing for the attachment cell:

@implementationMyDividerTextAttachmentCell

- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position characterIndex:(NSUInteger)charIndex
{
	return NSMakeRect(lineFrag.origin.x, lineFrag.origin.y, textContainer.size.width, 10);
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(nullable NSView *)controlView
{
	[[NSColor darkGrayColor] setStroke];
	
	[NSBezierPath strokeLineFromPoint:NSMakePoint(0, NSMidY(cellFrame)) toPoint:NSMakePoint(NSMaxX(cellFrame), NSMidY(cellFrame))];
}

[Q] Is there an obvious reason why there would be a huge gap (think 200 pixels/points) below the divider? What is the recommended way to render a divider line in a NSTextView if it's not NSTextAttachmentCell.

Are you actually running on macOS 10.13?

I tried to do this using NSTextAttachmentViewProvider and I couldn't get it to work. That requires macOS 12. But apparently, this TextKit 1/2 logic is quite flaky and delicate. So Perhaps you're actually running on TextKit 2 and that's what you should be seeing due to some bug. Once something is a lightly documented at custom text attachments, it's risky.

I'll try again with an NSTextAttachmentCell. Just because views are (theoretically) supported doesn't mean the cell shouldn't still work.

I think i found an answer for you. Someone asked the exact same question on Stack Overflow 14 years ago: https://stackoverflow.com/questions/8558853/nstextattachmentcell-is-a-mile-high

But I still have to debug my own text views. It looks like I may be inadvertenly triggering a TextKit 1 fallback. Thanks for opening this can-o-worms for me. It's better that I find out about it now before I ship.