我似乎无法让剪切、复制、粘贴的execCommand在contentEditable设置为true的UIWebView中工作。我可以使用selectAll命令选择文本,但剪切、复制和粘贴不起作用。
这是我使用的代码:
[webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('cut', false, null)"];要让剪贴板操作正常工作,我还需要做些什么吗?
发布于 2013-05-18 16:44:06
根据前面的评论,剪切、复制、粘贴命令似乎不起作用,所以我设法实现了相同的操作,如下所示:
- (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 = @"";
}发布于 2013-05-18 16:11:23
我不能百分之百确定,但我不相信在UIWebView中以编程方式调用剪切、复制或粘贴是可能的。但是,您可以使用window.getSelection()获取所选文本。您可以使用此处的选择内容执行您想要的操作。
https://stackoverflow.com/questions/16622032
复制相似问题