1. 程式人生 > >如何在webview中設定自定義字型

如何在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): <html><head><styletype="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格式的。