How to change font face of Webview in Android?

前端 未结 14 1146
北荒
北荒 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:53

    You need not use local ttf file to set webview font for android, It will increase the whole app size.

    you can also use online fonts like google's font... It will help to reduce your apk size.

    For example: visit the below URL https://gist.github.com/karimnaaji/b6c9c9e819204113e9cabf290d580551#file-googlefonts-txt

    You will found necessary info to use this font. then create a String like below

    String font = "@font-face {font-family: MyFont;src: url(\"here you will put the url of the font which you get previously\")}body {font-family: MyFont;font-size: medium;text-align: justify;}";
    

    then load the webview like this

    webview.loadDataWithBaseURL("about:blank", "<style>" +font+</style>" + "your html contnet here", "text/html", null, null);
    

    done. You will able to load the font from the external source in this way.

    Now whats about the app font, it doesn't make sense to use 1 font for the webview and different font for the whole app. So you can use Downloadable Fonts in your app which also uses external sources for loading fonts in the app.

    0 讨论(0)
  • 2020-11-22 12:56

    Even simpler, you can use the Asset URL format to reference the font. No programming needed!

    @font-face {
       font-family: 'myface';
       src: url('file:///android_asset/fonts/myfont.ttf'); 
    } 
    
    body { 
       font-family: 'myface', serif;
    

    ...

    0 讨论(0)
提交回复
热议问题