I want to change the font of the WebView. There are 2 files in the "assets" folder. It:
assets / fonts / DejaVuSans.ttf and
assets / CSS / result.css
I searched for some answers on StackOverflow and figured out a solution, but it does not work. Result .css loads DejaVuSans.ttf using the @ font-face directive. Then it is included in the <link> data tag, which is passed to WebView :: loadDataWithBaseURL (code below). The problem is with CSS styles (text red), but the font is not . Square characters appear when I show phonetic characters (/ 'pə: sn /).
String resultText = "<html><head>"; resultText += "<link href=\"css/result.css\"" + " rel=\"stylesheet\" type=\"text/css\" />"; resultText += "<style>" + "@font-face { font-family: \"DejaVuSans\"; " + "" + "src: url('file:///android_asset/fonts/DejaVuSans.ttf'); }</style>"; resultText += "</head>"; resultText += "<body><p>" + text + "</p>" + "</body></html>"; resultView.loadDataWithBaseURL("file:///android_asset/", resultText, "text/html", "utf-8", null);
result.css:
@font-face { font-family: "DejaVuSans"; src: url('file:///android_asset/fonts/DejaVuSans.ttf'); } body { color: red; }
System.out.println (resultText):
<html><head><link href="css/result.css" rel="stylesheet" type="text/css" /><style>@font-face { font-family: "DejaVuSans"; src: url('file:///android_asset/fonts/DejaVuSans.ttf'); }</style></head><body><p>person /'pə:sn/</p></body></html>
source share