Deselect text in NSTextView programmatically?

I am using the following code to deselect NSTextView as suggested here . Unfortunately, nothing happens. I tried what I know to debug it, but everything seems to work correctly, but it does not affect the NSTextView .

Code:

 // Sets the scrolling bounds and behavior. This might be useful, but I don't know [[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)]; [[textView textContainer] setWidthTracksTextView:FALSE]; // The code for deselecting, beginning by making sure it is actually selected (for testing only, as strange as it is) [textView setSelectable:TRUE]; [textView setDelegate:self]; [_window makeFirstResponder:textView]; NSText *fieldEditor = [_window fieldEditor:TRUE forObject:textView]; [fieldEditor setSelectedRange:NSMakeRange([[fieldEditor string] length],0)]; [fieldEditor setNeedsDisplay:YES]; 

Any ideas on why this is not working? I am sure my outputs are set correctly, because I can manipulate other things like string value.

+4
source share
6 answers

I'm not sure if NSTextViews uses a field editor, have you tried calling the method directly in a text view?

 [textView setSelectedRange:NSMakeRange(textView.string.length, 0)]; 

The range can be adjusted, for example, to move the cursor to the beginning or end. You can also check to make sure that something is actually selected before calling this method.

EDIT:

From your comment, it sounds like you just want him to abandon the first responder. You can do this manually by calling [textView.window makeFirstResponder:nil];

+6
source

It almost worked for me;

 [textView.window makeFirstResponder:nil]; 

However, I had problems setting the first responder to zero. If I set it up for any other view, it seems to do what you need.

 [textView.window makeFirstResponder:[textView superview]]; 

Tested at 10.7 lev.

+5
source

I use this approach and it works great:

 [textView setSelectedRange:NSMakeRange(0, 0)]; 
+2
source

As suggested earlier, setSelectedRange: will do the BUT trick!

If your goal is to completely remove the selection and cursor, fe if you subclass NSTextView to support similar behavior like NSTextEdit , in case of changing the status of firstResponder you should write:

 - (BOOL)resignFirstResponder { // Invalid range location will remove cursor too [self setSelectedRange:NSMakeRange(NSUIntegerMax, 0)]; return YES; } //------------------------------------------------------------------------------ - (BOOL)becomeFirstResponder { [self setSelectedRange:NSMakeRange(0, self.string.length)]; return YES; } //------------------------------------------------------------------------------ 
+2
source
 [textView setDelegate:self]; 

I have a feeling that one of your delegation methods is preventing something from happening. See the documentation in the "Selection Management" section.

-2
source

As a temporary solution, only until someone offers a better idea can setHidden: be used. I am sure that this is not as effective as recommended, but does not cancel the NSTextView .

Just switch it twice:

 [textView setHidden:TRUE]; [textView setHidden:FALSE]; 
-2
source

Source: https://habr.com/ru/post/1382201/


All Articles