如何在webview中设置自定义字体


  • In your assets/fonts folder, place the desired OTF or TTF font (here MyFont.otf)
  • Create a HTML file that you'll use for the WebView's content, inside the assets folder (here insideassets/demo/my_page.html):


     type="text/css">
    @font-face {
        
    font-family: MyFont;
        
    src: url("file:///android_asset/fonts/MyFont.otf")
    }
    body 
    {
        
    font-family: MyFont;
        
    font-size: medium;
        
    text-align: justify;
    }



    Your text can go here! Your text can go here! Your text can go here!


  • Load the HTML into the WebView from code:
    webview.loadUrl("file:///android_asset/demo/my_page.html");

Take note that injecting the HTML through loadData() has not worked for me. I'd be curious to hear if anybody can get that working.

需要注意的是,这种方式,只能使用.otf格式的字体文件,不能使用ttf格式的。

你可能感兴趣的:(Android)