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

落花浮王杯 提交于 2019-12-05 23:56:46

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]];

An NSText can have more than only one selection. Check it out with TextEditapp: select a string with the mouse while pressing CMD. So you can select as many strings as you want. Therefore I think, a more common solution is to use:

NSArray *ranges = [myTextView selectedRanges];

and then extract the strings one by one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!