How to change font face of Webview in Android?

前端 未结 14 1293
北荒
北荒 2020-11-22 12:23

I want change the default font of webview to a custom font. I\'m using webview in developing an bilingual browser app for Android.

I tried getting an instance of cus

14条回答
  •  無奈伤痛
    2020-11-22 12:30

    i done this by upper answers with this additions:

    webView.loadDataWithBaseURL("file:///android_asset/",
                                WebClient.getStyledFont(someText),
                                "text/html; charset=UTF-8", null, "about:blank");
    

    and then use src: url("file:///android_asset/fonts/YourFont...

    public static String getStyledFont(String html) {
        boolean addBodyStart = !html.toLowerCase().contains("");
        boolean addBodyEnd = !html.toLowerCase().contains("@font-face {font-family: CustomFont;" +
                "src: url(\"file:///android_asset/fonts/Brandon_reg.otf\")}" +
                "body {font-family: CustomFont;font-size: medium;text-align: justify;}" +
                (addBodyStart ? "" : "") + html + (addBodyEnd ? "" : "");
    }
    


    thanks to everybody:)

提交回复
热议问题