1
UIWebView.butで選択した単語を取得したい場合があります。は、uiwebviewで選択した単語を取得できません
私は、私は単語を選択し、範囲を変更した場合、選択した単語(getSelectedword.js)
var selectedText = "";
function getTextSelection() {
var text = window.getSelection();
selectedText = text.anchorNode.textContent.substr(text.anchorOffset, text.focusOffset - text.anchorOffset);
}
、次いで
-(void)showHeightlight
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"getSelectedword" ofType:@"js"];
NSError *error = nil;
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView stringByEvaluatingJavaScriptFromString:jsCode];
[webView stringByEvaluatingJavaScriptFromString:@"getTextSelection()"];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"selectedText"];
NSLog(@"selected: %@", result);
}
を取得するためにJavaScriptを使用選択した単語を取得できませんでした。
非常に便利です。なぜ最初に動作しなかったのか知っていますか? – Ricardo