Search code examples
objective-ccocoanstextfield

How do I check if there is a selection?


I have a NSTextView and I need to check if there is a selection, (blue highlight) of a word (or anything really), and not just a cursor. How can I do this. nil doesn't work, and I can't figure it out.


Solution

  • You can use [NSTextView selectedRanges] method to see if there is a selection.

    if (self.textView.selectedRanges.count > 0) {
        NSLog(@"Some text is selected!");
    }
    

    You may want to read the documentation for more information.