开发者

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

开发者 https://www.devze.com 2023-02-17 11:07 出处:网络
开发者_开发百科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, a
开发者_开发百科

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

精彩评论

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