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

假装没事ソ 提交于 2019-12-04 07:59:01

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;
    }
},
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!