Persian font in android App - android

I create application for android with phonegap.
Front-end of app designed with html, css and javascript. I have font folder under assets folder in project and use #font-face in css file for applying font.(My font is persian)
When the section of design of project (html,css,js) run in browser is true and fonts apply.
When project android compile this fonts don't apply for text but apply for numbers.
Please help me.
Thanks for your help.

Add your font.ttf in assets folder of your android application.
Then, For Example,
CODE
wv = (WebView) findViewById(R.id.webView1);
String st = "<html><head><style type=\"text/css\">#font-face {font-family:MyFont;src: url(\"file:///android_asset/font/font.ttf\")}body {font-family:MyFont;font-size: medium;text-align: justify;}</style></head><body>";
String ed = "</body></html>";
String myHtmlString = st + YourText + ed;
wv.loadDataWithBaseURL(null,myHtmlString, "text/html", "UTF-8", null);

Put your fonts in directory called "fonts" under your PhoneGap www folder.
Using CSS include your fonts like this (I have used Roboto Fonts here in this example):
#font-face {
font-family: 'Roboto Condensed';
font-style: normal;
font-weight: 400;
src: local('Roboto Condensed Regular'), local('RobotoCondensed-Regular'), url(../fonts/RobotoCondensed-Regular.ttf) format('truetype');
}
Wherever you want to apply Roboto fonts, apply like this in css: i.e.
font: 400 1.25em/45px 'Roboto Condensed', sans-serif;
Note that, For Android Version greater than 4 it can Use bundled Fonts.
However for Android Version < 4 need to download fonts From Web like this:
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic' rel='stylesheet' type='text/css'>
Hope this helps..

Related

Android Error getting when load Font from asset folder

I am loading CSS from Asset folder. It’s working fine except font loading. I am getting following error while leading font.
"Failed to decode downloaded font: file:///android_asset/css/seriff.ttf"
Here my code,
#font-face {
font-family: 'MyFont';
src: url('seriff.ttf');
}
body {
font-family: 'MyFont';
background-color: #FFBB00;
}
content = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/style.css\"><body>" + "Testing something" + "</body></html>";
newsView.loadDataWithBaseURL("file:///android_asset/", content, "text/html", "utf-8", null);
Other CSS property is working fine. Please help if anyone have idea to fix this.
Thanks.
use relative path to the font
src: url('../fonts/seriff.ttf');
and to the css
href=\"./css/style.css\"
It is likely that your ttf font is not compatible with the Android WebView. To confirm this replace your seriff.ttf file with another ttf file and see if it is displayed correctly. 
You can use a ttf converter to read in the font then write it back out it should fix the issue. A site such as freefontconverter.com will help with this task.

Custom Persian And Arabic Fonts in Android 4.1.2 Phonegap (Cordova) not working

I tried to make custom fonts for my application in phonegap. For that, I wrote this code in my html file:
#font-face {
font-family: 'customfont';
src: url('fonts/BMitra.eot?#') format('eot'),
url('fonts/BMitra.woff') format('woff'),
url('fonts/BMitra.ttf') format('truetype');
font-style: normal;
}
body {
font-family: "customfont";
font-size:30px;
}
....
<body>
<h>Custom Fonts</h>
<div style="font-family: 'customfont';" id='txt1'>This is for sample</div>
</body>
It works fine on Ripple (chrome emulator) but it doesn't work on the Device (Samsung Android 4.1.2)
I have googled and stack-overflowed to find the solution, i found this post font-face on android 4.0.x doesn't work which says to have text-rendreing:auto but it didn't work too.
Any help, will be appreciated.
finally I overcome this issue on Persian And Arabic custom fonts,
Keep all the codes with #fontface as same as it is,
Download this package http://averta.net/labs/fa/?p=10
From extracted archive, add bifon-1.1b.js to your html and use this script
function onDeviceReady() {
$('#txt1').text(FarsiStyle.convert('سلام عليكم').split('').reverse().join('').split(' ').reverse().join(' '));
}
which txt1 is a div or span.
p.s Thanks to Mr. Morteza F. Shojaei

webview font not getting applied

I have an android webview and the text in it has a font of sans-serif. I have the ttf file in assets/sans-serif.ttf
Here is the code
WebView webVw;
String webContent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webVw=(WebView)findViewById(R.id.webVw);
webContent="%3Cdiv%20style%3D%22width%3A100%25%3Bheight%3A100%25%3Bposition%3Arelative%3Bleft%3A.1%25%3Btop%3A.1%25%22%3E%3Cdiv%20style%3D%22position%3Aabsolute%3Bwidth%3A31%25%3Bheight%3A42.22222222222222%25%3Bleft%3A36.375%25%3Btop%3A19.02313624678663%25%3Bcolor%3Argb(0%2C%200%2C%200)%3Bfont-size%3A50px%3Bfont-style%3Anormal%3Bfont-family%3Asans-serif%2C%20sans-serif%2C%20sans-serif%3Bfont-weight%3A400%22%20%3EOverlay!%20%3C%2Fdiv%3E%3Ca%20href%3D%22javascript%3A%20void(0)%22%20%20target%3D%22_blank%22%20%3E%3Cimg%20src%3D%22https%3A%2F%2Fsi0.twimg.com%2Fprofile_images%2F1490344068%2FLOGO_COGNIZANT_C_only_normal.jpg%22%20style%3D%22position%3Aabsolute%3Bwidth%3A4.875%25%3Bheight%3A10.444444444444445%25%3Bleft%3A0.625%25%3Btop%3A1.2853470437017995%25%22%20%2F%3E%3C%2Fa%3E%3Cdiv%20style%3D%22position%3Aabsolute%3Bwidth%3A4%25%3Bheight%3A6.666666666666667%25%3Bleft%3A95%25%3Btop%3A1.5424164524421593%25%3Bbackground%3Argb(47%2C%206%2C%20246)%20none%22%20%3E%3C%2Fdiv%3E%3Cdiv%20class%3D%22cf-component%20buttonwrap%22%20style%3D%22position%3Aabsolute%3Bborder%3Dradius%3A5px%3Bwidth%3A4.25%25%3Bheight%3A4.222222222222222%25%3Bleft%3A6.125%25%3Btop%3A3.3419023136246784%25%3Bbackground%3Argb(6%2C%20115%2C%20233)%3Bfont-size%3A15px%3Bfont-style%3Anormal%3Bfont-family%3Asans-serif%2C%20sans-serif%2C%20sans-serif%3Bfont-weight%3A400%22%20%3E%3Ca%20%20class%3D%22editableTxt%22%20href%3D%22http%3A%2F%2Fwww.cognizant.com%22%20target%3D%22_blank%22%3E%20Go!%3C%2Fa%3E%3C%2Fdiv%3E%3C%2Fdiv%3E";
String head="<head><style>#font-face {font-family: 'sans-serif';src: url('file:///assets/sans-serif.ttf');}body {font-family: 'sans-serif';}</style></head>";
String HtmlString = "<html>"+head+"<body>"+webContent+"</body></html>";
webVw.loadData(HtmlString, "text/html","charset=UTF-8");
}
The webview displays the normal font but not sans-serif
Try this...
In your assets/fonts folder, place the desired OTF or TTF font (here MyFont.otf)
Create a HTML file that you'll use for the WebView's content, inside the assets folder (here inside assets/demo/my_page.html):
<html>
<head>
<style type="text/css">
#font-face {
font-family: MyFont;
src: url("file:///android_asset/fonts/MyFont.otf")
}
body {
font-family: MyFont;
font-size: medium;
text-align: justify;
}
</style>
</head>
<body>
Your text can go here! Your text can go here! Your text can go here!
</body>
</html>
Load the HTML into the WebView from code:
webview.loadUrl("file:///android_asset/demo/my_page.html");
Take note that injecting the HTML through loadData() has not worked for me. I'd be curious to hear if anybody can get that working.
use this code instead of it
webVw.loadDataWithBaseURL("file:///android_asset/",HtmlString, "text/html", "UTF-8",null);
In some cases if you tried many ways to solve this problem and you did not get your problem solved yet. you should try to check your font file correct name.open your font file and check font name, copy that and rename file to correct name.
In your html codes use correct name too.
I had this problem several times!

Custom Fonts in Android PhoneGap

I tried to make custom fonts for my application. For that, I wrote this code in my html file:
<style type="text/css" media="screen">
#font-face {
font-family: centurySchoolbook;
src: url(/fonts/arial.ttf);
}
body {
font-family: centurySchoolbook;
font-size:30px;
}
</style>
In my Html Body:
<body onload="init();">
<h>Custom Fonts</h>
<div>This is for sample</div>
</body>
But, these styles are not applied to my html body..
Any help to solve this..??
I made it work after doing the following steps:
-Put your CSS in a file, for example my_css.css:
#font-face {
font-family: "customfont";
src: url("./fonts/arial.ttf") format("opentype");
/* Make sure you defined the correct path, which is related to
the location of your file `my_css.css` */
}
body {
font-family: "customfont";
font-size:30px;
}
-Reference your CSS file my_css.css in your HTML:
<link rel="stylesheet" href="./path_to_your_css/my_css.css" />
Pay attention to the definition of your paths!
For example, let's say you have the following directory structure:
www
your_html.html
css
my_css.css
fonts
arial.ttf
Then, you would have:
#font-face {
font-family: "customfont";
src: url("../fonts/arial.ttf") format("opentype");
/* Make sure you defined the correct path, which is related to
the location of your file `my_css.css` */
}
body {
font-family: "customfont";
font-size:30px;
}
and:
<link rel="stylesheet" href="./css/my_css.css" />
Note: if you use jQuery Mobile, the solution may not work (jQuery Mobile will "interfere" with the css...).
So, you may try to impose your style by using the style attribute.
Eg:
<body>
<h>Custom Fonts</h>
<div style="font-family: 'customfont';">This is for sample</div>
</body>
One drawback is that it seems that the style cannot be applyied on body...
So, if you want to apply the font to the whole page, you may try something like this:
<body>
<!-- ADD ADDITIONAL DIV WHICH WILL IMPOSE STYLE -->
<div style="font-family: 'customfont';">
<h>Custom Fonts</h>
<div >This is for sample</div>
</div>
</body>
Hope this will work for you too. Let me know about your results.
Below works like charm for custom font with jQueryMobile and Phonegap:
#font-face {
font-family: "Lato-Reg";
src: url("../resources/Fonts/Lato-Reg.ttf") format("opentype");
/* Make sure you defined the correct path, which is related to
the location of your file `my_css.css` */
}
body *{
margin: 0;
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
font-family: "Lato-Lig" !important;
font-weight: normal !important;
}
I also faced the same problem. I put my css file in the css folder. I put the ttf file in just the www folder and it didn't work properly, so I moved my ttf file into the css folder. Here is my code:
#font-face
{
font-family: CustomArial;
src: url('arial.ttf');
}
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
font-family: CustomArial;
}
If you are using jquery mobile, you have to override the font like:
.ui-bar-a, .ui-bar-a input, .ui-bar-a select, .ui-bar-a textarea, .ui-bar-a button {
font-family: CustomArial !important;
}
You have to override the font-family in your css for whatever jquery mobile class having font-family:Helvetica, Arial, sans-serif; to
font-family:CustomArial !important;
Now it will work. You can try like this, may be its useful for you.
here your can find the .ttf the google fonts : https://github.com/w0ng/googlefontdirectory
in your .css file
#font-face {
font-family: 'Open Sans';
src: url('../font/OpenSans-Regular.ttf') format('truetype');
font-weight: 400;
}
I was facing a similiar issue. The problem was with the path it was taking when the font was given through an external css file.
To resolve this, i declared the font URL inline in the body of index.html
<div>
<style scoped>
#font-face {
font-family: Monotype Corsiva;
src: url('fonts/Monotype_Corsiva.ttf') format('truetype');
}
</style>
</div>
It worked after declaring the font URL in this manner.
<style type="text/css" media="screen">
#font-face {
font-family: 'centurySchoolbook';
src: url('fonts/arial.ttf');
}
body {
font-family: centurySchoolbook;
font-size:30px;
}
</style>
with quotes on the font-family and on the url worked for me, inside the html file and on Android.
It works on physical devices, but not on emulator.
The possible issues lies in default index.css in cordova. Check if the body element has style defined for "text-transform:uppercase".
Atleast that was the issue for me, after removing this from body element in default index.css if you are using in your application, may help you as well.
For me I was using gurmukhi/punjabi fonts, and after the above line removal from index.css it just worked like charm with below css definiitions only
Example :
.demo {
font-family: anmol
}
#font-face {
font-family: anmol;
src: url(../fonts/anmol.ttf);
}
I just copied my ttf-fonts into the directory [app-name]/css and declared the font-families in the index.css there. See attached image: my edited index.css (Check the green marked areas in the attached picture). There i also changed the style "background-attachment:fixed;" "Helvetica, Arial, no-serif…" to "Open Sans, Open Sans Condensed, no-serif", – i also deleted the line "text-transform:uppercase". After that everything worked fine with my font "Open Sans".
Of course i also included my own stylesheet with declarations of the other styles and font-families of my project.
I am also using jQuery and jQueryMobile in that project. They use the same font (Open Sans), but i added additional fonts that work as well!
Happy coding!

after webview used custom fonts

I'm currently developing an android web browser. There has some pages used other character like Uighur character, and they display unnormally, so I used the following code to resolved this problem.
String htmlStr = getHTML(url, "UTF-8");//get the html content from the url
String tmp="<link href=\"file:///android_asset/myfont.css\" rel=\"stylesheet\" type=\"text/css\" />";
if(htmlStr.contains("</head>")){
htmlStr = htmlStr.replace("</head>", tmp);//add my css into the page
}
mCurrentWebView.loadDataWithBaseURL(url, htmlStr,"text/html", "utf-8", "");//display the page content
This is myfont.css:
#font-face { font-family: MyCustomFont; src: url("fonts/ALPEKRAN.TTF") }
body {font-family: MyCustomFont, Verdana, Arial, sans-serif; text-align:right;}
The ALPEKRAN.TTF font is under the folder of assets/fonts/.
But there have another problem like that page css losed.
Please, give me some suggession.
May you need to specify the full font url, try:
src: url("file:///android_asset/fonts/ALPEKRAN.TTF")

Categories

Resources