现在我想显示一些 unicode 字符并且我使用了 tag: <font face="
Arial">something here</font>
。但似乎WebView
找不到 Arial 字体,因为我只能看到 UFO 字符。我是否必须将 arial.ttf 复制到某个地方,或者如何将此 TrueType 字体与WebView
. 谢谢。
10 回答
loadData
对我也不起作用,所以我file:///android_asset
在 src 路径中使用。
它与loadDataWithBaseURL
!
对于此示例,我将 CSS 更改为:
@font-face {
font-family: 'feast';
src: url('fonts/feasfbrg.ttf');
}
body {font-family: 'feast';}
然后使用资产路径作为基本 url:
loadDataWithBaseURL("file:///android_asset/",myhtml,"text/html","utf-8",null);
显然,您可以WebView
按照上面的@raychung 建议使用自定义字体。但这不适用于 2.1(此处报告了错误)。这应该适用于 1.5、1.6 和 2.2。
您可以将自定义字体 TTF 文件放在/assets
文件夹中,然后在 CSS 文件中放入:
@font-face {
font-family: "myIPA";
src: url('IPA.TTF');
}
.phon, .unicode
{
display: inline;
font-family: 'myIPA', Verdana, sans-serif;
font-size: 14pt;
font-weight: 500;
font-style:normal;
color: black;
}
您现在可以在 HTML 文件中引用此字体样式。
@font-face {
font-family: 'MyCustomFont';
src: url('/assets/fonts/MaycustomFont.ttf')
}
这个对我有用。
->src
->assets
->fonts
->MaycustomFont.ttf
->..
->res
您可以通过在应用程序首次启动时将字体文件从资产复制到您的文件夹来使其适用于所有版本,然后将其引用为:
"@font-face {
font-family: cool_font;
src: url('file://"+ getFilesDir().getAbsolutePath()
+ "/cool_font.ttf');
}"
我在下面的代码中使用了波斯字体的 rtl 方向,希望有人使用此代码或有人建议我最好的方法。谢谢
String myCustomStyleString="<style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/BYEKAN.ttf\")}body,* {font-family: MyFont; font-size: medium;text-align: justify;}</style>";
webView.loadDataWithBaseURL("", myCustomStyleString+"<div style=\"direction:rtl\">"+myHtm+"</div>", "text/html", "utf-8", null);
它对我不起作用,我不得不在我的网络服务器上链接字体,而不是使用对本地文件的引用:
@font-face {
font-family: 'feast';
src: url('http://yoursite.com/tutorial/css/feasfbrg.ttf');
}
body {font-family: 'feast';}
This works great for me on all the devices I've tested it on.
Place your font files in /src/main/assets/fonts, then use this method:
public class HTMLUtils {
public static String getLocalFontInHTML(String html, String fontFamily, String fontFileName) {
return "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<style>" +
"@font-face {" +
"font-family: " + fontFamily + ";" +
"src: url('" + fontFileName + "');" +
"}" +
"* {font-family: '" + fontFamily + "' !important;}" +
"* {font-size: 1rem !important;}" +
"</style>" +
"</head>\n" +
"<body>\n" +
html +
"\n" +
"</body>\n" +
"</html>";
}
}
as follows
webView.loadDataWithBaseURL("file:///android_asset/", HTMLUtils.getLocalFontInHTML(item.text, Config.FONT_FAMILY, Config.REGULAR_FONT),"text/html", "UTF-8", null);
where
public static final String FONT_FAMILY = "Montserrat";
public static final String REGULAR_FONT = "fonts/Montserrat-Regular.otf";
Hope it helps someone!
If you want to keep the font size from your html code remove
"* {font-size: 1rem !important;}"
Have in mind that 1rem is equivalent to about 14sp
正如 Felipe Martinez Carreño 所说,您可以从资产中复制,它会起作用。
您可以参考this了解更多详情
基本思想是网页应该可以访问字体文件。这就是为什么当 HTML 文件和字体文件都在 assets 文件夹中并且从那里加载 HTML 文件时建议的原始解决方案有效的原因。
它不是 Android 功能,它是 CSS,因此应该适用于任何 Android SDK。
如果字体文件确实存在并且样式中给出了正确的路径,那么从文件夹加载字体也应该可以工作。但是这种方法比较麻烦,需要编写代码来复制文件,然后编写代码来确定文件的位置。
我很高兴在资产中简单地包含 HTML 内容和字体文件,并从那里加载。
webView.loadUrl("file:///android_asset/content.html");
使用 CSS
@字体脸{ 字体家族:cool_font; src: url('cool_font.ttf'); }