Android webview custom font using javascript

Viewed 172

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>
0 Answers
Related