I am showing some local web content and would like to get the word under the jump point. The only delegate member is set up, and I can make it work fine if I use UITextView and load HTML using NSAttributedString and NSHTMLTextDocumentType, but I lose formatting like paragraph spacing and small caps; adding NSMutableParagraphStyle has no effect.
Using UIWebView and loadHTMLString, which includes jQuery, is there any way? There are several pages that show how to use JS to get the word under the cursor, for example:
How to get word under cursor using JavaScript?
How to get a word under the cursor?
Get the word under the mouse pointer
Is it possible to get a word under the mouse cursor in `<textarea>`?
How to get a word under the cursor?
But after a few days I could not get it to work. My code is:
var jsFile = NSBundle.mainBundle().pathForResource("webview", ofType: "js");
let jsData = NSString(contentsOfFile:jsFile!, encoding:NSUTF8StringEncoding, error:nil) as! String
let returned = webView.stringByEvaluatingJavaScriptFromString(jsData)! as String
println("Returned \(returned).")
A word may be unicode with a different range of code, but getting any word to work would be great at first.
source
share