7

我正在尝试在NSTextField.

我发现了一篇类似的文章,但对于NSTextView. 我通过覆盖NSTextField和放置来尝试类似的代码:

- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard
{
    return [super readSelectionFromPasteboard: pboard];
}

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

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

4

6 回答 6

3

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

于 2013-07-26T21:39:36.173 回答
2
  1. 覆盖becomeFirstResponder你的方法NSTextField

  2. 用于object_setClass覆盖“字段编辑器”的类(NSTextView处理所有NSTextField实例的文本输入;请参见此处

#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
  1. 创建你的MyFieldEditor类并覆盖它的paste:方法
@interface MyFieldEditor : NSTextView
@end

@implementation MyFieldEditor

- (void)paste:(id)sender
{
  // Get the pasted text.
  NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
  NSString *text = [pasteboard stringForType:NSPasteboardTypeString];
  NSLog(@"Pasted: %@", text);

  // Set the pasted text. (optional)  
  [pasteboard clearContents];
  [pasteboard setString:@"Hello world" forType:NSPasteboardTypeString];

  // Perform the paste action. (optional)
  [super paste:sender];
}

@end

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

于 2019-02-20T01:09:07.430 回答
1

结合此处找到的 2 个答案,我能够为我找到解决方法,但有必要将NSTextField,NSTextFieldCellNSTextView.

斯威夫特 5

1.

Subclass 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.

子类化NSTextFieldCellfieldEditorFor使用您自己的NSTextView. 重要的是设置true为.isFieldEditorNSTextView

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您自己的静态属性NSTextFieldCellNSTextFieldCell如果您只是将自己的分配给 all ,则可以避免最后一步NSTextField,即NSTextField.cellClass = yourCellClass

final class TextField: NSTextField {
    required init?(coder: NSCoder) {
        nil
    }

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

于 2021-02-16T17:19:07.093 回答
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
于 2018-10-08T07:11:01.633 回答
-1

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

在这里查看我的答案:

NSTextField:公开其复制和粘贴方法

于 2016-06-17T15:51:36.650 回答
-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;
}
于 2016-01-20T07:21:16.467 回答