Created
November 6, 2017 12:58
-
-
Save mksantoki/703a49c97c96f7e82dde6239cea26499 to your computer and use it in GitHub Desktop.
Android webview with custom font
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.webkit.WebView; | |
/** | |
* Created by mauliksantoki on 6/11/17. | |
*/ | |
public class WebviewUtil { | |
// Reference https://forums.xamarin.com/discussion/40192/how-to-do-a-webview-renderer-with-custom-font-on-android | |
/** | |
* This clase is use to apply custom fonts in webview. | |
* | |
* @param htmlString HtmlString data | |
* @param path This path is your custom fonts android_asset path Like this file:///android_asset/fonts/century_gothic.ttf | |
* @return true if the move is valid, otherwise false | |
* @version V1.0 | |
*/ | |
public static String setCustomFont(String htmlString, String path) { | |
return "<html>" + | |
"<head>" + | |
"<style type=\"text/css\">" + | |
"@font-face {" + | |
"font-family: MyFont;" + | |
"src: url(\"" + path + "\")" + | |
"}" + | |
"body {" + | |
"font-family: MyFont;" + | |
"font-size: medium;" + | |
"text-align: justify;" + | |
"}" + | |
"</style>" + | |
"</head>" + | |
"<body>" + htmlString + | |
"</body>" + | |
"</html>"; | |
} | |
/** | |
* @param htmlString HtmlString data | |
* @param fileName your ccs File name | |
* @return true if the move is valid, otherwise false | |
* @version V1.0 | |
* <p> | |
* Note:- if your css file structure like android_asset/ccs/style.css here href will href="css/style.css" menas after android asset folder path will your href value | |
*/ | |
public static String setCssStyle(String htmlString, String fileName) { | |
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"fileName.css\" />" + htmlString; | |
} | |
/** | |
* @param htmlString HtmlString data | |
* @param webView your ccs File name | |
* @return true if the move is valid, otherwise false | |
* @version V1.0 | |
* <p> | |
* Note:- if your css file structure like android_asset/ccs/style.css here href will href="css/style.css" menas after android asset folder path will your href value | |
*/ | |
public static void loadString(WebView webView, String htmlString) { | |
webView.loadDataWithBaseURL("file:///android_asset/", htmlString, "text/html", "UTF-8", null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment