开发者

Is it possible to programmatically invoke "select all" then "copy" on a UIWebView on iPhone?

开发者_开发百科

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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜