0

When I apply custom font in assets/font to local html. I just set fontface like below @font-face { font-family: ‘my’font; src: url('file:///android_asset/fonts/my’font); } However, above code is not working, when I use loadUrl(“remote html url”). Have a nice Idea?

Below link show sample code for local font and local html. How to change font face of Webview in Android?

However I need remote html and local font sample.

sonicmario
  • 601
  • 8
  • 22
  • See if this answer from a similar question helps you? https://stackoverflow.com/questions/3854966/android-how-to-reference-asset-images-from-a-remotely-loaded-html-page-in-webvi – Bubalubs Nov 06 '20 at 08:18

1 Answers1

0

Handle font-face With jsoup


Hello sir you can handle your webview with jsoup

First of all you need implementation jsoup implementation 'org.jsoup:jsoup:1.11.1'

then you need fetch your site like this Document doc = Jsoup.connect("http://youramazing.site").get();

after that you can add your custom style to your document with append method

        doc.head().append(" <style>\n" +
                "            @font-face {\n" +
                "            font-family: 'Roboto Slab Bold';\n" +
                "            src: url('file:///android_asset/fonts/syne.ttf')\n" +
                "            }\n" +
                "            body { font-family: 'Roboto Slab Bold', serif; font-size: 17px; color: #000; }\n" +
                "            a { color: #000; }\n" +
                " </style>");

after that you can handle it with webview String s = doc.html() webView.loadData(s,"text/html","utf-8");

Full code with AsyncTask:

public class MainActivity extends AppCompatActivity {
    WebView webView;
    Document doc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.wewewew);
        AsyncTask<Void,Void,String> asyncTask = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {
                try {
                    doc = Jsoup.connect("http://youramazing.site/").get();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                doc.head().append(" <style>\n" +
                        "            @font-face {\n" +
                        "            font-family: 'Roboto Slab Bold';\n" +
                        "            src: url('file:///android_asset/fonts/syne.ttf')\n" +
                        "            }\n" +
                        "            body { font-family: 'Roboto Slab Bold', serif; font-size: 17px; color: #000; }\n" +
                        "            a { color: #000; }\n" +
                        " </style>");
                return doc.html();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                webView.setWebViewClient(new WebViewClient());
                webView.loadData(s,"text/html","utf-8");
            }
        };
        asyncTask.execute();



    }
}
MoriX
  • 1
  • 2