问题
I'm trying to handle a paste operation on a NSTextField.
I found a similar article but for NSTextView. I tried code similar to this by overriding NSTextField and putting:
- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard
{
return [super readSelectionFromPasteboard: pboard];
}
But this method seems to never be called.
Any suggestions on how to detect a past on NSTextField?
回答1:
You could use the NSTextFieldDelegate delegate method - (BOOL) control:(NSControl*) control textView:(NSTextView*) textView doCommandBySelector:(SEL) commandSelector and watch for the paste: selector.
回答2:
Overriding NSTextFieldCell and putting.
///////////////////////////////////////////
//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:
Override the
becomeFirstRespondermethod of yourNSTextFieldUse
object_setClassto override the class of the "field editor" (which is theNSTextViewthat handles text input for allNSTextFieldinstances; see here)
#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
- Create your
MyFieldEditorclass and override itspaste:method
@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
All done! Now you can intercept every paste action.
回答4:
A nstextfield does not have copy and paste functions. Those are only found in nstextview. the catch is that when a textfield is edited it opens up a textview called a fieldeditor during the editing.
See my answer here:
NSTextField: exposing its Copy and Paste methods
回答5:
Here is what i use to detect paste in 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;
}
来源:https://stackoverflow.com/questions/17881770/detect-paste-on-nstextfield