是否可以在 UIWebview ios 中更改文本选择颜色? [英] Is it possible to change text selection color in UIWebview ios?

查看:19
本文介绍了是否可以在 UIWebview ios 中更改文本选择颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have load html file in UIwebview and had put below code in html file.

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

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

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

and also set tint color in Uiwebview but still it is showing blue color when i am selecting text.

推荐答案

是的.我浪费了 2 天来解决这个令人头疼的问题.访问这个链接并将 JS 代码放入您的 JS 中文件.将其导入到 .HTML 文件中.

Yes. I wasted 2 days to resolve this headache issue.Visit This Link and Place JS Code in your JS File.Import it in .HTML file.

这是它的示例代码.

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代码调用这个方法

call this method by Objective-C code

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

这篇关于是否可以在 UIWebview ios 中更改文本选择颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆