2011-07-17 32 views
6

我想更改WebView字體。 「資產」文件夾中有2個文件。它們是:使用CSS更改WebView字體;資產文件夾中的字體文件。 (問題)

資產/字體/ DejaVuSans.ttf和

資產/ CSS/result.css

我搜索StackOverflow上一些答案,找出一個解決方案,但它不工作。 result.css使用@ font-face指令加載DejaVuSans.ttf。然後它被包含在傳遞給WebView :: loadDataWithBaseURL的數據的鏈接>標記<中(代碼如下)。 問題是CSS樣式的作品(文字是紅色),但字體不是。當我顯示拼音符號時出現方形字符(/'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; 
} 

的的System.out.println(resultText)爲:

<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> 

回答

16

它被解決了。我沒有「FONT-FAMILY」添加到body選擇和字體url必須的文件:///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; 
} 
+0

我在windows上工作。 ame知道「字體家族」? – MobiDev

+0

我有同樣的問題,但字體仍然不會加載。我在加載到Web視圖的HTML文件中編寫整個文本文件和字體。 – Kenji

4

已知的錯誤上2.0和2.1:

http://code.google.com/p/android/issues/detail?id=4448

2.2 :

@font-face { 
    font-family: 'FontName'; 
    src: url('filefont.ttf'); // with no "fonts/" 
} 
+0

我用標籤「的Android 2.2」。該錯誤是2.0,它是固定在2.2。 – Emerald214

+0

您的權利,編輯... – ChristopheCVB

+0

這是行不通的。我的字體文件在資產/字體文件夾。我試圖將它複製到資產/但仍然不起作用。 – Emerald214

0

我用的Cufón更換爲不支持的設備。我的本地字體。 我寫了一個javascript函數來確定的Cufón是否需要

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; 
    } 
}, 
相關問題