我有一个NSTextView,用户可以将纯文本粘贴到其中。
当用户在粘贴板中有"foo“时,我希望"bar”被粘贴。换句话说,用户去,比方说,网络浏览器,选择"foo",cmd+c,切换到my NSTextView,cmd+v,并且"bar“出现在插入点。
有没有人知道怎么解决这个问题?
编辑:有没有可能以某种方式使用readSelectionFromPasteboard:type:实现这一点?我只是不知道在我被覆盖的文本视图的方法体中放什么?
发布于 2012-08-14 04:21:06
尝尝这个。继承你的文本视图,然后覆盖paste:这样:
@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];
}
}
@endhttps://stackoverflow.com/questions/11937726
复制相似问题