UIWebView execCommand剪切,复制,粘贴
我似乎无法得到execCommand剪切,复制,粘贴工作在一个UIWebView的contentEditable设置为true。 我可以使用selectAll命令select文本,但剪切,复制和粘贴不起作用。
这是我正在使用的代码:
[webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('cut', false, null)"];
有什么我需要做的,以允许剪贴板操作工作?
按照以前的评论削减,复制,粘贴命令似乎没有工作,所以我设法实现相同的操作,如下所示:
- (void)cut { NSString *selection = [self.webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; [webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('delete', false, null)"]; [UIPasteboard generalPasteboard].string = selection; } - (void)paste { NSString *text = [UIPasteboard generalPasteboard].string; [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('insertHTML', false, '%@')", text]]; [UIPasteboard generalPasteboard].string = @""; }
我不是100%确定的,但我不相信可以在UIWebView中以编程方式调用剪切,复制或粘贴。 但是,您可以使用window.getSelection()获取选定的文本。 你可以从那里做select。