简体   繁体   English

如何使命令-A在NSTableView中的行中选择所有NSTextView文本?

[英]How can I make command-A select all the NSTextView text in rows in an NSTableView?

So if I have an NSView based tableview and inside the views are NSTextViews which are non-editable but selectable... 因此,如果我有基于NSView的tableview,并且视图内部是NSTextViews,它们是不可编辑但可选择的......

how can I get that nice functionality of command-A selects all the text? 如何才能获得命令A的优秀功能选择所有文本? I don't mean row selection. 我不是指行选择。 I have row selection disabled for the tableview. 我为tableview禁用了行选择。 I mean highlighting the text in blue so you can copy it to your clipboard. 我的意思是用蓝色突出显示文本,以便将其复制到剪贴板。 But not just 1 NSTextView's text from one row, all of them from all the rows. 但不只是1行NSTextView的文本,所有行都来自所有行。

And in addition to command-A click and drag should do this too. 除了命令之外,点击和拖动也应该这样做。 But out of the box it seems I can only select one row's text. 但开箱即用似乎我只能选择一行的文字。 Here is video showing problem: 这是显示问题的视频:

https://dl.dropboxusercontent.com/u/2510380/table.mov (i keep clicking and dragging but can't highlight text on the next row) https://dl.dropboxusercontent.com/u/2510380/table.mov (我一直点击并拖动但不能突出显示下一行的文字)

here are two mac apps (skype and gabble) that do this: 这里有两个mac应用程序(skype和gabble)执行此操作:

https://dl.dropboxusercontent.com/u/2510380/skype.mov https://dl.dropboxusercontent.com/u/2510380/skype.mov

and

https://dl.dropboxusercontent.com/u/2510380/gabble.mov https://dl.dropboxusercontent.com/u/2510380/gabble.mov

Assuming they are NOT using WebViews with just HTML inside, how do you get this control over the clipboard? 假设他们不使用仅包含HTML的WebViews,那么如何通过剪贴板获得此控制? ie in Skype you select the text and only the conversation is highlighted, not the timestamp of each message. 即在Skype中,您选择文本,仅突出显示会话,而不是每条消息的时间戳。 Also the text copied to the clipboard is formatted very nicely. 复制到剪贴板的文本格式也非常好。 Can you point me in the right direction to reverse engineer skype? 你能指出我正确的方向来反向设计skype吗?

Unfortunately there's no way to do this easily. 不幸的是,没有办法轻易做到这一点。 This is because only ONE control can be the first responder at a time. 这是因为一次只有一个控件可以是第一个响应者。 This means that, though you can have selection in multiple text views, there are several problems: 这意味着,尽管您可以在多个文本视图中进行选择,但存在以下几个问题:

  1. Only one text view's text will actually be highlighted with the "live" highlight color; 实际上只有一个文本视图的文本会以“实时”高亮颜色突出显示; the others will have the gray highlight of non-focused controls. 其他人将拥有非聚焦控件的灰色亮点。

  2. Copy commands will only apply to the first responder text view. 复制命令仅适用于第一个响应者文本视图。

  3. Drag session starts will be initiated from the control the mouse was actually pointing at (irrespective of first responder) and will only drag that control's text. 拖动会话启动将从鼠标实际指向的控件启动(与第一响应者无关),并且仅拖动该控件的文本。

  4. In a view-based table view, the controls may not even "exist" for a row not currently displayed, so it'll never get the message unless you forcibly create each row, which could be costly for a large table. 在基于视图的表视图中,对于当前未显示的行,控件可能甚至“不存在”,因此除非您强行创建每一行,否则它将永远不会获取消息,这对于大型表来说可能是昂贵的。

Knowing all this, you might be able to "fake it" by having your controller be complicit in a text view and table view subclass's special handling of a select-all message when it's first responder. 知道了这一切,你可能能够通过让你的控制器在文本视图和表视图子类中对第一响应者的select-all消息进行特殊处理来“伪装”它。 On receiving this message, the text view subclass can call super then notify the controller (to get its default behavior AND to let you know it happened), at which point the controller can turn around and send the command to all (existing) text views. 收到此消息后,文本视图子类可以调用super然后通知控制器(获取其默认行为并让您知道它发生了),此时控制器可以转向并将命令发送到所有(现有)文本视图。 Highlighting can be spoofed by overriding the text view's drawing and a drag initiation could defer to a delegate (the controller), which would handle writing ALL the strings from your model to the pasteboard (not even touching the text views in possibly-nonexistent row views). 通过覆盖文本视图的绘图可以欺骗突出显示,并且拖动启动可以推迟到委托(控制器),该委托将处理将模型中的所有字符串写入粘贴板(甚至不会触及可能不存在的行视图中的文本视图) )。 The table view subclass would simply pass the same select-all message to the controller without calling super (and even forcibly making sure nothing is selected before returning for good measure). 表视图子类只是将相同的select-all消息传递给控制器​​而不调用super(甚至强制确保在返回for good measure之前没有选择任何内容)。

I hope this helps. 我希望这有帮助。 If I've forgotten any of your requirements, let me know. 如果我忘记了您的任何要求,请告诉我。

Try like this:- 试试这样: -

First create button programatically then write this code after you create button and also write this code in your load method or awakefromnib method. 首先以编程方式创建按钮,然后在创建按钮后编写此代码,并在load方法或awakefromnib方法中编写此代码。

   NSButton *Buttn=// alloc initwithframe;

  [Buttn setKeyEquivalentModifierMask: 
NSCommandKeyMask];
[Buttn setKeyEquivalent:@"A"];
[Buttn     
setAction:@selector(yourmeth:)];
 [Buttn setTarget:self];

 // now when you press cmd a write 
 below code in action method
- (void)selectRowIndexes:(NSIndexSet
 *)indexes byExtendingSelection:
   (BOOL)extend

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM