Android webview custom font using javascript

亡梦爱人 提交于 2021-02-10 16:00:42

问题


I am trying to show text in webview with custom font which i have selected to show that font style. So following details explain the problem in code

public void setFontFamily(String fontFamily) {
        exec("javascript:RE.setFontFamily('" + fontFamily + "');");
    }

So In JS file 

I have following the code

RE.setFontFamily = function(fontFamily) {
    RE.restorerange();
    //document.execCommand("insertHtml", false, "&nbsp")
    console.log("Font Family: "+fontFamily)
    document.execCommand("styleWithCSS", null, true);
    document.execCommand("fontName", false, fontFamily);
    document.execCommand("styleWithCSS", null, false);
};

Above is code snippet i have used

So calling methods are below mentioned

 when (p2) {
            0 -> {
                richEditor.setFontFamily("Times New Roman, Times, serif")
            }
            1 -> {
                richEditor.setFontFamily("Trebuchet MS, Helvetica, sans-serif")
            }
            2 -> {
                richEditor.setFontFamily("Verdana, Geneva, sans-serif")
            }
            3 -> {
                richEditor.setFontFamily("Comic Sans MS, Cursive, sans-serif")
            }
            else -> {
                richEditor.setFontFamily("Courier New, Courier, monospace")
            }

In this above call Times New Roman and Courier New is working correctly but other fonts are not working. Important thing is html is correctly compiled from my input

Compiled Html data is below 


<font face="Times New Roman, Times, serif">Times new Roman, </font><div><font face="Trebuchet MS, Helvetica, sans-serif">trebuchet, </font></div><div><font face="Verdana, Geneva, sans-serif">verdana, </font></div><div><font face="Comic Sans MS, cursive, sans-serif">comic sans</font></div><div><font face="Times New Roman, Times, serif">Times new Roman, </font></div><div><font face="Trebuchet MS, Helvetica, sans-serif">trebuchet, </font></div><div><font face="Verdana, Geneva, sans-serif">verdana, </font></div><div><font face="Comic Sans MS, cursive, sans-serif">comic sans</font></div>

来源:https://stackoverflow.com/questions/65108865/android-webview-custom-font-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!