font styles not working when set custom font for webview - 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

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.

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.

Related

How to show Myanmar font from database in webview

I store Myanmar font in database. When I show Myanmar font by webview, They show the followings. How do I change my codes? Please help me.
ပရမá€á± (ပရမ + အá€á³) = အá‚ွစ္á€á€ºá€³á€•á€¹áŠ အျမင့္ဆုံး သိစရာá‹
These are my codes.
dstory = (WebView) findViewById(R.id.dstory);
String data=result.getString(3);
dstory.loadData(data , "text/html", "utf-8");
Try adding Fonts to your Asset folder and add it to your WebView by CSS
You Code Should be loaded from asset directory
webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);
and your CSS should be
#font-face {
font-family: 'my_font';
src: url('my_font.ttf');
}
#Muthu Thank u for your advice.Now I got it.
In css file,
#font-face {
font-family: 'ZawgyiOne2008';
src: url('file:///android_asset/ZawgyiOne2008.ttf');
}
body{
font-family: ZawgyiOne2008; width: 100%;
background-color: transparent;
padding: 0px;
}
In my DetailActivity.java, to link css is used full html.
WebView view = (WebView)findViewById(R.id.webView1);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html =
"<html><head><LINK href=\"file:///android_asset/bootstrap.css\" type=\"text/css\" rel=\"stylesheet\"/></head><body><H1 class='label'>A simple HTML page</H1>" +
"<input type='text' class='input-large' required='true' id='cname' name='cname'/>" +
"<input type='text'/>" +
"<p>The quick brown fox jumps over the lazy dog</p></body></html>";
view.loadDataWithBaseURL("", html, mimeType, encoding, "");
To show Myanmar Font from database, use string replace.
String old="%#";
String newstr=html.replace(old, data);

android- font face is not changing on webview

I need to change webview font face , I've searched and I used this but it didn't change the font , I've the font file in asset folder :
wb.loadData(
"<html><head><style>#font-face { font-family: myface;src: url('file:///android_asset/yekan.ttf');" +
"BODY, HTML {background: transparent; } body,div { font-family: myface;} </style></head><body><div style='text-align: justify; line-height: 23px;float:right' dir='rtl'>"
+ text + "</div></body></html>",
"text/html; charset=utf-8", "UTF-8");
is it wrong or webview couldn't change the font face ?
thanks
change
url('file:///android_asset/yekan.ttf')
to
url(\"file:///android_asset/yekan.ttf\")

Arabic font issue in the Android WebView

I am trying to load a HTML file into android webview. I have fonts and html folder inside the assests. In the fonts folder I have Arabic Regular.otf and Arabic Bold.otf. On loading the page into webview i can't see the difference between bold and regular fonts. I have searched in SO, and then changed the font from otf to svg format. It works fine for me in Jelly Bean, but not exactly in HoneyComb. Please Help.
My styles in HTML file.
.c0{direction:rtl}
p span.medium { font-family:"AdobeArabic-Bold", sans-serif; }
p span { font-family:"AdobeArabic-Regular", sans-serif; }
#font-face {
font-family: 'AdobeArabic-Regular';
src: url('file:///android_asset/fonts/AdobeArabic-Regular.svg');
}
#font-face {
font-family: 'AdobeArabic-Bold';
src: url('file:///android_asset/fonts/AdobeArabic-Bold.svg');
}
And my Activity Code;
WebView wv=(WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/html/policy.html");
And my HTML code is
<p class="c0"><span class="medium">المعلومات التي تقدمها.</span></p>
<p class="c0"><span>لكي تتمكن من تصفح هذا الموقع، يجب عليك إدخال بعض المعلومات الشخصية المعرفة (مثل الإسم والبريد الإلكتروني وتاريخ الميلاد). بهدف أن تستفيد من بعض المزايا المميزة. سوف يتم إعلامك أية معلومات مطلوبة منك وأيها اختياري ولأي هدف سنستخدمها.</span></p>
Try something like this, setting them as the same family, but different weights:
#font-face {
font-family: "AdobeArabic";
src: url('file:///android_asset/fonts/AdobeArabic-Regular.svg');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: "AdobeArabic";
src: url('file:///android_asset/fonts/AdobeArabic-Bold.svg');
font-weight: bold;
font-style: normal;
}
And then:
p span {
font-family: "AdobeArabic-Regular", sans-serif;
}
p span.medium {
font-weight: bold;
}

Android Jsoup change font-family in style tag

I need to change font-family to a custom one after loading html into WebView. Basically I need to get this:
<style>
#font-face {
font-family: 'MyFont';
src: url('file:///android_asset/Custom-Font.otf')
}
body { font-family: 'MyFont', serif; font-size: 17px; color: #000; }
a { color: #000; }
</style>
from
<style>
body { font-family: 'Helvetica', serif; font-size: 17px; color: #000; }
a { color: #000; }
</style>
Html is dynamically loaded, not from assets. I use Jsoup, but cannot get this font-family parameter so far...
JSoup does not parse the CSS for you. It ends up in a DataNode hanging off the style element. From there, you are on your own in terms of parsing. If you have a simple search and replace, you can use a regular expression to do the work. For complicated situations, you can look at a [CSS Parser][1]. Your situation looks relatively simple, but given just one exemplar it is hard to tell which elements you want preserved. Here is a sketch of the basic idea, you may need to modify to fully fit your situation. The basic idea is to get the DataNode from the Style tag and using a regex to search and replace the field.
final String newFontInfo = "#font-face { \n"
+ "font-family: 'MyFont';\n"
+ "src: url('file:///android_asset/Custom-Font.otf')\n" + "}\n";
Elements styles = doc.select("style");
for (Element style : styles) {
for (DataNode data : style.dataNodes()) {
String dataTxt = data.getWholeData();
if (dataTxt.contains("font-family")) {
final String newData = dataTxt.replaceAll("font-family:\\s*'[^']*'","font-family: 'MyFont'");
data.setWholeData(newFontInfo + newData);
}
}
}

Change WebView font using CSS; font file in asset folder. (problem)

I want to change WebView font. There are 2 files in "assets" folder. They are:
assets/fonts/DejaVuSans.ttf and
assets/css/result.css
I searched some answers on StackOverflow and find out a solution but it doesn't work. The result.css loads DejaVuSans.ttf using #font-face directive. Then it is included in < link > tag of data which is passed to WebView::loadDataWithBaseURL (The code is below). The problem is CSS styles works (the text is red) but the font doesn't. Square characters appears when I show phonetic symbols (/'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('fonts/DejaVuSans.ttf'); also not work*/
src: url('file:///android_asset/fonts/DejaVuSans.ttf');
}
body {
color: red;
}
The System.out.println(resultText) is:
<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>
It was solved. I didn't add "font-family" to body selector and font url must be file:///android_asset/fonts/DejaVuSans.ttf".
#font-face {
font-family: 'DejaVuSans';
/* src: url('DejaVuSans.ttf'); This doesn't work */
/* src: url('fonts/DejaVuSans.ttf'); Neither is this */
src: url('file:///android_asset/fonts/DejaVuSans.ttf'); /* but this does */
}
body {
font-family: 'DejaVuSans';
color: red;
}
Known bug on 2.0 and 2.1 :
http://code.google.com/p/android/issues/detail?id=4448
For 2.2 :
#font-face {
font-family: 'FontName';
src: url('filefont.ttf'); // with no "fonts/"
}
I used cufon replacement for devices that didn't support my local font face.
I wrote a javascript function to determine whether cufon is required
requiresCufon : function(){
var version = window.navigator.userAgent.match(/Android\s+([\d\.]+)/);
var MINIMUM_OS_FONTFACE = 2.2;
if(typeof version != 'undefined'){
//strip non digits
version = version[0].replace(/[^0-9\.]/g, '');
//if version is less than required for #font-face then cufon
if(parseFloat(version) < MINIMUM_OS_FONTFACE)
return true;
else
return false;
}
else{
return true;
}
},

Categories

Resources