Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

android - Custom font for webview

I am trying to establish a new custom font (otf file) for my web view.

I put anbaaarabic_bold.otf in assets file.

There is how i do:

String head = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ getActivity().getFilesDir().getAbsolutePath()+ "/anbaaarabic_bold.otf');}body {font-family: 'verdana';}</style></head>";
String htmlData= "<html>"+head+"<body style="font-family: verdana">"+body+"</body></html>" ;

mBodyArticle.loadDataWithBaseURL("http://nada", htmlData,
                    "text/html", "utf-8", "");

body : contain my html code.

I don't know why the font is not working for my webview. body html font doesn't change.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a bug report for Android that seems to suggest that custom fonts don't work for certain languages (Thai, Hebrew, Farsi, and Arabic have been mentioned). This hasn't been confirmed by anyone from Google, but there are a number of people reporting the same problem.

One of the suggested solutions (which at least one user confirmed as working) was to convert the font from otf to svg. There are a number of online tools that will do this for you (Everything Fonts is one example), but you may have issues with this being a copyrighted font - make sure your font license permits such conversions.

Also, when you have converted the font, open it up in a text editor and check whether the svg element is namespaced. It should look something like this:

<svg xmlns="http://www.w3.org/2000/svg">

If it is just <svg> without the namespace, you may need to add the xmlns attribute manually. Apparently, svg fonts without the namespace don't work either.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...