Sequential choice between multiple NSTextViews

I have a bunch of NSTextView that I would like to share one choice. I basically want it to be like selecting text on a webpage where there are several text presentations, but you can drag them to select text among them sequentially.

I found this document stating that it is possible to have multiple NSTextContainer objects having one NSLayoutManager , and thus share the choice, This is halfway to what I want, except for the fact that one NSLayoutManager can only have one NSTextStorage object. I want each text view to have its own NSTextStorage so that each text view can have its own text, but I still want to be able to select text in multiple text views with a single drag and drop. Is it possible?

+4
source share
2 answers

There is no easy way to solve this problem (as I tried to find by asking this question). It includes all the mouse event processing and text selection calculations that you expect, so I wrote the code and opened it as an INDSequentialTextSelectionManager .

+4
source

In order for these separate text containers to work, you calculate the size of the picture for each part of the line and limit the NSTextView to this size:

  NSLayoutManager * layout = [[NSLayoutManager alloc] init]; NSString * storedString = @"A\nquick\nBrown\nFox"; NSTextStorage * storage = [[NSTextStorage alloc] initWithString:storedString]; [storage addLayoutManager:layout]; //I assume you have a parent view to add the text views NSView * view; //Assuming you want to split up into separate view by line break NSArray * paragraphs = [storedString componentsSeparatedByString:@"\n"]; for (NSString * paragraph in paragraphs) { NSSize paragraphSize = [paragraph sizeWithAttributes:@{}]; //Create a text container only big enough for the string to be displayed by the text view NSTextContainer * paragraphContainer = [[NSTextContainer alloc] initWithContainerSize:paragraphSize]; [layout addTextContainer:paragraphContainer]; //Use autolayout or calculate size/placement as you go along NSRect lazyRectWithoutSizeOrPlacement = NSMakeRect(0, 0, 0, 0); NSTextView * textView = [[NSTextView alloc] initWithFrame:lazyRectWithoutSizeOrPlacement textContainer:paragraphContainer]; [view addSubview:textView]; } 

You can add a delegate to NSLayoutManager to view the use of the text container:

 - (void)layoutManager:(NSLayoutManager *)aLayoutManager didCompleteLayoutForTextContainer:(NSTextContainer *)aTextContainer atEnd:(BOOL)flag { if (aTextContainer == nil) { //All text was unable to be displayed in existing containers. A new NSTextContainer is needed. } } 
0
source

All Articles