我有一个 NSTextView,用户可以将纯文本粘贴到其中。
当用户在粘贴板中有“foo”时,我想粘贴“bar”。换句话说,用户转到网络浏览器,选择“foo”,cmd+c,切换到我的 NSTextView,cmd+v 并且“bar”出现在插入点。
请问,有谁知道如何处理这个问题吗?
编辑:是否可以为此使用 readSelectionFromPasteboard:type:
?我只是不知道要在我覆盖的 textview 的方法体中放入什么......?
最佳答案
试试这个。子类化您的 TextView ,并覆盖粘贴:这样:
@implementation RDTextView
-(void)paste:(id)sender {
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSString *pbItem = [pb readObjectsForClasses: @[[NSString class],[NSAttributedString class]] options:nil].lastObject;
if ([pbItem isKindOfClass:[NSAttributedString class]])
pbItem = [(NSAttributedString *)pbItem string];
if ([pbItem isEqualToString:@"foo"]) {
[self insertText:@"bar"];
}else{
[super paste:sender];
}
}
@end
关于objective-c - NSTextView粘贴: how to paste "bar" instead when "foo" is pasted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937726/