2

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
jbrennan
  • 11,943
  • 14
  • 73
  • 115
  • Try take a look at `NSLayoutManager` and `NSTextContainer`. – TheAmateurProgrammer Apr 29 '13 at 08:04
  • @TheAmateurProgrammer Yeah I've looked at those before posting this. There's a ton of meat in the text system and just knowing the classes hasn't been too helpful just yet. – jbrennan Apr 30 '13 at 13:52
  • Yeah, I've tried out the 2 classes before, but couldn't get it to work the way I wanted. Apple did had an example that showcased a textview layed out in a circle using those 2 classes, so maybe try look at the source of that and see what you can get out of it. – TheAmateurProgrammer Apr 30 '13 at 23:19

1 Answers1

2

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)
Jonathan H.
  • 939
  • 1
  • 7
  • 21