Laying out text in NSTextView around a subview

488 Views Asked by At

What I’m trying to do is have an NSTextView and add custom NSView subviews to it, but have it so the text can layout around the subviews.

Right now, I can easily add a subview to the textview but of course, that goes into the textview and the text is ignorant of the subviews, so it just runs over/under the subview. Not what I’d like, of course.

So I’d like to be able to add my own views at least in “block” (like when an HTML element is a block element, so it’s on its own line), and maybe “inline” as well (that is, a subview in line with the text, just like how an HTML element can be inline) although this is not absolutely required.

I can’t quite figure out how to make this work. The only way I’ve seen that things can be added to a textview are with Text Attachments, but those seem relegated to only images/files, and only in an NSCell, which doesn’t contain (to my knowledge) an arbitrary NSView.

I feel like this should be possible though, where do I start?

1

There are 1 best solutions below

0
On

I know it's been quite some time since this question was posted, but I recently encountered the same issue and found the solution to be very straightforward.

Simply append an exclusion path to the NSTextView's container, which is accomplished like so in Swift:

// Where 'subview' is the view you would like to avoid drawing text over
let exclusionPath = NSBezierPath(rect: subview.frame)
self.textContainer?.exclusionPaths.append(exclusionPath)