开发者

How to get highlighted text from my NSTextField?

开发者 https://www.devze.com 2023-03-08 19:57 出处:网络
It must be certainly easy but I haven\'t found anything in the doc. The behavior I need is exactly as when you right-click any portion of text and then you can do some action with it.

It must be certainly easy but I haven't found anything in the doc. The behavior I need is exactly as when you right-click any portion of text and then you can do some action with it. For the moment I have a my own custom NSTextField class which re-implment 'mouseDown' action. This part works ! I thought I could get the selected portion of text of my nstextfield thanks to 'theEvent' but apparent开发者_如何学Cly it is not possible.


I don't actually think you can do this with NSTextField. I think you have to use an NSTextView instead and then make use of the -selectedRanges method.

EDIT: I should have said, you can't do this directly (i.e., there's no NSTextField method for doing this). I think rather, you have to use the field editor (which is itself an NSTextView) associated with the window in order to do this. Here's the apple guide for using the field editor.


NSString *selectedText = ((NSTextFieldCell*)textField.selectedCell).stringValue;
0

精彩评论

暂无评论...
验证码 换一张
取 消