安卓WebView自定义字体

8

我在我的应用程序中使用Webview,希望使用定制字体显示波斯语文本。我将我的字体放在asset文件夹中,并编写了一个在Webview中放置文本的方法。但在不支持波斯语的API 8中,它会显示未知字符,而在支持波斯语的API中,它不会使用自定义字体显示我的文本,而是使用默认字体显示。请问我的方法哪里有问题?谢谢。 我的方法:

  public void setText(String text)
{
    String style2="<style type=\"text/css\">@font-face {font-family: 'myface';src: url('file:///android_asset/BYekan.ttf'); } body {font-family: 'myface';}</style>";
    this.loadDataWithBaseURL("file:///android_asset/BYekan.ttf","<html  dir=\"rtl\"><head><meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>"+style2+"</head><body>"+text+"</body></html>","text/html","UTF-8",null);
    setClickable(false);
    setLongClickable(false);
    setFocusable(false);
    setFocusableInTouchMode(false);
}

只是猜测,尝试将 file:///android_asset 作为您的基础文件夹,而不是 file:///android_asset/BYekan.ttf。 - Hussain Akhtar Wahid 'Ghouri'
我测试了,但是不起作用 :( @HussainAkhtarWahid - user2857021
1个回答

1
你的样式应该看起来像这样:

@font-face{ 
font-family:myface;
font-weight:normal;
font-style:normal;
src:url('file:///android_asset/BYekan.ttf') format('truetype');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接