您的位置:首页 > 移动开发

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

2015-09-28 15:46 501 查看
In your assets/fonts folder, place the desired OTF or TTF font (here MyFont.otf)

[align=left]Create a HTML file that you'll use for the WebView's content, inside the assets folder (here insideassets/demo/my_page.html):[/align]
<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>

[align=left]Load the HTML into the WebView from code:[/align]
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格式的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: