Search code examples
objective-cxcodemacosnsstringnstextview

Get selection (highlighted text) string from NSTextView Objective-C


How can I get the selected text's string from a NSTextView as an NSString?

Your help is greatly appreciated.


Solution

  • Since NSTextView is a subclass of NSText, you can use NSText instance methods to figure out the selected string like so:

    NSString *selected = [[myTextView string] 
                          substringWithRange:[myTextView selectedRange]];