是否可以在 UIWebview ios 中更改文本选择颜色?

Is it possible to change text selection color in UIWebview ios?

我已在 UIwebview 中加载 html 文件,并将以下代码放入 html 文件中。

 <style>
  ::-moz-selection { /* Code for Firefox */
              color: red;
              background: yellow;
          }

      ::selection {
          color: red;
          background: yellow;
      }
      </style>

还在 Uiwebview 中设置了色调,但当我选择文本时它仍然显示蓝色。

是的。我浪费了 2 天来解决这个令人头疼的问题 issue.Visit 这个 Link 并将 JS 代码放入你的 JS File.Import .HTML 文件中.

这是它的示例代码。

function highlight(colour) {
var range, sel;
if (window.getSelection) {
    // IE9 and non-IE
    try {
        if (!document.execCommand("BackColor", false, colour)) {
            makeEditableAndHighlight(colour);
        }
    } catch (ex) {
        makeEditableAndHighlight(colour)
    }
} else if (document.selection && document.selection.createRange) {
    // IE <= 8 case
    range = document.selection.createRange();
    range.execCommand("BackColor", false, colour);
}}

通过Objective-C代码调用此方法

[webView stringByEvaluatingJavaScriptFromString:@"highlight('#ff0')"];