检测NSTextField上的粘贴

时间:2013-07-26 12:50:43

标签: macos cocoa nstextfield

我正在尝试处理NSTextField上的粘贴操作。

我找到了similar article,但NSTextView。我通过覆盖NSTextField和put:

尝试了与此类似的代码
- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard
{
    return [super readSelectionFromPasteboard: pboard];
}

但这种方法似乎永远不会被调用。

有关如何在NSTextField上检测过去的任何建议?

6 个答案:

答案 0 :(得分:3)

您可以使用NSTextFieldDelegate委托方法- (BOOL) control:(NSControl*) control textView:(NSTextView*) textView doCommandBySelector:(SEL) commandSelector并关注paste:选择器。

答案 1 :(得分:1)

结合在这里找到的 2 个答案,我能够找到适合我的解决方法,但有必要对 NSTextFieldNSTextFieldCellNSTextView 进行子类化。

Swift 5

1.

子类NSTextView,这里是截取实际粘贴的地方,可以替换内容。

final class TextView: NSTextView {
    override func paste(_ sender: Any?) {
        var content = NSPasteboard.general.string(forType: .string) ?? ""

        // modify content

        NSPasteboard.general.clearContents()
        NSPasteboard.general.setString(content, forType: .string)
        super.paste(sender)
    }
}

2.

子类 NSTextFieldCell 并用您自己的 fieldEditorFor 覆盖 NSTextView。将 true 的属性 isFieldEditor 设置为 NSTextView 很重要。

final class TextFieldCell: NSTextFieldCell {
    private let textView = TextView()
    
    required init(coder: NSCoder) {
        super.init(coder: coder)
    }

    override init(textCell: String) {
        super.init(textCell: textCell)
        textView.isFieldEditor = true
    }
    
    override func fieldEditor(for: NSView) -> NSTextView? {
        textView
    }
}

3.

子类 NSTextField 并将您自己的 cellClass 分配给静态属性 NSTextFieldCell。如果您只是将自己的 NSTextFieldCell 分配给所有 NSTextField,即 NSTextField.cellClass = yourCellClass

,则可以避免这最后一步
final class TextField: NSTextField {
    required init?(coder: NSCoder) {
        nil
    }

    init() {
        TextField.cellClass = TextFieldCell.self
        super.init(frame: .zero)
    }
}

答案 2 :(得分:0)

覆盖NSTextFieldCell并放置。

 ///////////////////////////////////////////
 //BPastTextFieldCell.h
 //
 @interface BPastTextView : NSTextView <NSTextViewDelegate>
 @end

 @class BPastTextFieldCell ;
 @interface BPastTextFieldCell : NSTextFieldCell

 @end




  //////////////////////////////////////////
  //
  //BPastTextFieldCell.m 
  //

  #import "BPastTextFieldCell.h"

  @implementation BPastTextFieldCell

  - (NSTextView *)fieldEditorForView:(NSView *)controlView{
     BPastTextView *textView = [[BPastTextView alloc] init];
      return textView;
  }
  @end




  @implementation BPastTextView

  - (void)keyDown:(NSEvent *)theEvent {

      bool bHandled = false;
      if ([theEvent modifierFlags] & NSEventModifierFlagCommand)
      {
          NSResponder * responder = [[self window] firstResponder];
          if ((responder != nil) && [responder isKindOfClass[NSTextView class]])
          {
              NSTextView * textView = (NSTextView *)responder;
              NSRange range = [textView selectedRange];
              bool bHasSelectedTexts = (range.length > 0);
              unsigned short keyCode = [theEvent keyCode];

              if (keyCode == 6)  //command + Z
              {
                  if ([[textView undoManager] canUndo])
                  {
                      [[textView undoManager] undo];
                      bHandled = true;
                  }
              }
              else if (keyCode == 7 && bHasSelectedTexts) // command + X
              {
                  [textView cut:self];
                  bHandled = true;
              }
              else if (keyCode== 8 && bHasSelectedTexts)  // command + C
              {
                  [textView copy:self];
                  bHandled = true;
              }
              else if (keyCode == 9)   // command + V
              {
                  [textView paste:self];
                  bHandled = true;
              }
          }
      }
      if(bHandled)
          return;

      [super keyDown:theEvent];

  }

  @end

答案 3 :(得分:0)

  1. 覆盖becomeFirstResponder的{​​{1}}方法

  2. 使用NSTextField覆盖“字段编辑器”的类(object_setClass处理所有NSTextView实例的文本输入; see here

NSTextField
  1. 创建您的#import <AppKit/AppKit.h> #import <objc/runtime.h> @interface MyTextField : NSTextField @end @implementation MyTextField - (BOOL)becomeFirstResponder { if ([super becomeFirstResponder]) { object_setClass(self.currentEditor, MyFieldEditor.class); return YES; } return NO; } @end 类并覆盖其MyFieldEditor方法
paste:

全部完成!现在您可以拦截每个粘贴操作。

答案 4 :(得分:-1)

nstextfield没有复制和粘贴功能。这些只能在nstextview中找到。问题在于,当编辑文本字段时,它会在编辑期间打开一个名为fieldingitor的文本视图。

请在此处查看我的回答:

NSTextField: exposing its Copy and Paste methods

答案 5 :(得分:-2)

以下是我用来检测UITextField中的粘贴的内容:

// Set this class to be the delegate of the UITextField. Now when a user will paste a text in that textField, this delegate will be called.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    // Here we check if the replacement text is equal to the string we are currently holding in the paste board
    if ([string isEqualToString:[UIPasteboard generalPasteboard].string]) {

        // code to execute in case user is using paste

    } else {

        // code to execute other wise
    }

    return YES;
}