I want to provide a copy button where the user can quickly copy the text from a UIWebView. When I hold my finger and move it to the edges, I have no problem selecting all of the text, and then hitting copy. Is there a way to programmatically do this?
I didn't try it, but this should work to get the already selected text:
NSString *webViewString = [webView stringByEvaluatingJavaScriptFromString:@"(function (){return window.getSelection().toString();})();"];
[[UIPasteboard generalPasteboard] setString:webViewString]; //Copy to pasteboard
NSString *pasteBoardString = [[UIPasteboard generalPasteboard] string]; //Paste from pasteboard
Just search for another javascript snippet to select all... You should find one very quickly.
You can get the text in the UIWebView with some javascript:
NSString *innerText = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
It will give you the HTML text, including tags and everything...
精彩评论