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.
I am trying to add Arabic custom fontto webView with no luck. In my case, webView renders the default font shipped with the device.
I have tried ttf and otf font types also converted font type to svg through https://everythingfonts.com with no luck
here is my code:
html file stored in the assets folder:
<html>
<head>
<meta http-equiv="content-type" content="text/html;" charset="UTF-8">
<style>
/** Specify a font named "MyFont",
and specify the URL where it can be found: */
#font-face {
font-family: "MyFont";
src: url('file:///android_asset/fonts/NotoKufiArabic-Bold.ttf') format(‘truetype’); ;
}
h3 { font-family:"MyFont"}
</style>
</head>
<body>
<h3>
مرحبا</h3>
</body>
</html>
MainActivity where i call webView:
public class MainActivity extends Activity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to WebView of the activity_main layout
mWebView = (WebView) findViewById(R.id.webview);
// Loading an html page into webview
mWebView.loadUrl("file:///android_asset/demo.html");
}
...........
...........
my code is working fine in case of English text and font but nothing for Arabic.
Use <h3 dir="rtl" lang="ar"> arabic text </h3> :
dir="rtl" inform that the text is arabix
lang="ar" renders form right to left
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..
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!
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")