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

来源:互联网 发布:易语言打开网页源码 编辑:程序博客网 时间:2024/04/29 01:10

  • 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):
    <html>
    <head>
    <style 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;
    }
    </style>
    </head>
    <body>
    Your text can go here! Your text can go here! Your text can go here!
    </body>
    </html>
  • 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格式的。

0 0