2013-07-18 32 views
2

我一直在努力突出顯示UIwebview中的文本。在這我使用canPerformAction方法,其中我調用一個JavaScript函數來突出顯示文本。讓我的webview中的文字是 「這部分是如此混亂,非常零星」。 在這裏,如果我單獨選擇文字「so」,沒有問題,它突出顯示文本。無法突出顯示UIWebview中的文本?

但讓我選擇超過1個詞,如「如此混亂」,它突出顯示,但它運行到一個無限循環,並繼續調用canPerformAction方法。我無法弄清楚它爲什麼會發生。

在iPad中,如果你長時間按下將被選中的文本。我的方法在這裏被調用並突出顯示選定的文本。然後,如果我拖動藍線來選擇更多的文本(我希望你能理解我在說什麼)在canPerformAction被調用,並且它運行到無限循環。

我正在模擬器中檢查。

更新: 我想你已經告訴那些東西如下

NSString *selectedString = @"function getSelText()" 
           "{" 
           "alert('document.execute');" 
           "var sel = window.getSelection();" 
           "if(!sel.isCollapsed){" 
           "var selRange = sel.getRangeAt(0);" 
           "document.designMode = 'on';" 
           "sel.removeAllRanges();" 
           "sel.addRange(selRange);" 
           "alert('document.execute 2');" 
           "document.execCommand('HiliteColor', false, '#ffffcc');" 
           "alert('document.execute 3');" 
           "sel.removeAllRanges();" 
           "document.designMode = 'off';" 
           "alert('document.execute 4');" 
           "}" 
           "}"; 
[webView stringByEvaluatingJavaScriptFromString:selectedString]; 

I have given the above code in webViewDidFinishLoad. 

In CanPerformAction I gave 
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{ 

    if(action == @selector(selectAll:)){ 

     NSString *setHighlightColorRuling = [NSString stringWithFormat:@"getSelText()"]; 
     [webView stringByEvaluatingJavaScriptFromString:setHighlightColorRuling]; 

     return YES; 

    } 
    return [super canPerformAction:action withSender:sender]; 

} 

雖然在模擬器中運行的警告,直到最後「document.execute 4」出現,但沒有在無影響網頁流量

回答