Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

font styles not working when set custom font for webview

Tags:

android

Main.java:

Main instanceMain = this;
    String Dataz = FN.getHtmlData(instanceMain,"BMitra",20,(getResources().getString(R.string.matn2)));
    btne.loadDataWithBaseURL("file:///android_asset/", Dataz, "text/html", "utf-8", null);

FN.java:

public static String getHtmlData(Context context,String fontname,Integer fontsize, String data){
    String head = "<head><style type=\"text/css\">@font-face {font-family: 'MYFNT';src: url('"+fontname+".ttf');} body {text-align:justify;font-family: 'MYFNT';color: Black;}</style></head>";
    String htmlData= "<html>"+head+"<body dir=\"rtl\">"+data+"</body></html>" ;
    return htmlData;
 }

strings.xml:

<string name="matn2">
    <![CDATA[
    <html><body><b>سلام</b> non bold text . <b>english bold</b></body></html>
    ]]>
</string>

but bold tag not working and output is : سلام english bold . non bold text

by the way i found some report issue


in another way :

Main.java :

Main instanceMain = this;
    String Dataz = FN.getHtmlData(instanceMain,getResources().getString(R.string.matn2)));
    btne.loadDataWithBaseURL("file:///android_asset/", Dataz, "text/html", "utf-8", null);

FN.java:

public static String getHtmlData(Context paramContext, String paramString)
  {
    return "<!DOCTYPE HTML><html lang=\"fa-ir\" dir=\"rtl\"><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head>" + "    <body>" + paramString + "</body></html>";
  }

justified_textview.css (in assest folder) :

/*@font-face {
font-family:"MyFont";
src:url('file:///android_asset/BMITRA.TTF');
}

@font-face {
font-family:"MyFont";
src:url('file:///android_asset/BMITRABD.TTF');
font-weight: bold;
}*/

@font-face {
font-family: 'Conv_BMITRA';
src: url('file:///android_asset/BMITRA.ttf');
font-weight: normal;
font-style: normal;
}

@font-face {
font-family: 'Conv_BMITRA';
src: url('file:///android_asset/BMITRABD.ttf');
font-weight: bold;
font-style: normal;
}


body {
font-family:'Conv_BMITRA';
text-align:justify;
line-height: 20pt;  
color: Black;      
}

and matn2(strings.xml) :

<string name="matn">
    <![CDATA[
    م ولادت توی وب با سی اس اس
    normal text
    <b>ولایت بولد م bold text</b>
    ]]>
    </string>

and now its work on android 2.3.3 or 2.3.5 but not working on android 4.0.x

like image 432
CooL i3oY Avatar asked Sep 13 '12 07:09

CooL i3oY


2 Answers

It's not possible, because of Webview rather than Android OS progress, is broken. If you set a custom font for Webview, in some device and Android OS it works and in some others not, unfortunately.

like image 174
Dr.jacky Avatar answered Oct 26 '22 23:10

Dr.jacky


I ran into the same issue on a really large page I was displaying. Try setting the activity with the webview to android:hardwareAccelerated="false" in the manifest.

like image 45
Chris Feist Avatar answered Oct 27 '22 01:10

Chris Feist