2013-08-07 45 views
0

爲什麼以下javascript命令不能在UIWebView html富文本編輯器中使用的RGB十六進制中設置字體顏色?使用execCommand在UIWebView中使用Javascript文本編輯器

顏色的RGB十六進制更改爲標準藍色,黃色,紅色等,例如:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('forecolor', false, '#407ec9')"]]; 

生成HTML

<font color="#0000ff"> HELLO WORLD </font> 

回答

0

解決了它。這是我的解決方法答案:

NSString *color = @"#407ec9"; 
NSString *text = [self.webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; 

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('insertHTML', false, '%@')", [NSString stringWithFormat:@"<font color=\"%@\">%@</font>", color, text]]]; 
+1

如果可能的話,我會建議使用一些不贊成使用的''標籤。 ''會更好。 – Luke

+0

好的建議謝謝。 – chongsj

+0

也許如果你有contenteditable屬性爲真,那麼它會工作 – meteors

0

在Chrome,Safari瀏覽器,Firefox中的以下工作和Opera,甚至Internet Explorer 9的:

<input type="button" onclick="document.execCommand('forecolor', false, '#407ec9');" value="Change" /> 
<div contenteditable="true">Test</div> 

http://jsfiddle.net/M5j8b/

它也可以在移動Safari瀏覽器中的iOS 6

,我沒有看到任何明顯的錯誤與您的通話stringByEvaluatingJavaScriptFromString。我懷疑有些東西沒有正確逃脫。

我會嘗試使在頁面上簡單的JavaScript函數,並調用它來代替:

function foo() { 
    document.execCommand('forecolor', false, '#407ec9') 
} 

如果不工作,我懷疑一個不同的問題。

+0

對不起,我在iOS UIWebView中使用該JS命令。我試圖簡化我的問題,現在編輯它以在iOS環境中正確地形成問題。這似乎是UIKit的一個問題。 – chongsj

+0

不起作用。我發佈了一個解決方法的答案。感謝您的幫助! – chongsj

+0

@chongsj奇。它應該有。你的頁面中一定有其他東西。那麼,很高興你想到了一些東西 – Luke