【问题标题】:Use Font-face in Android WebView在 Android WebView 中使用 Font-face
【发布时间】:2016-06-03 16:45:26
【问题描述】:

我想在我的 WebView 中插入字体,以便在不同的网站上显示一些特殊字体。

例如,在 Wikipedia 中,我希望我使用一些代码来更改 html 中的字体,完成后,我显示的页面和字体不像 html 代码中的 Century Gothic。当我尝试使用 Google Chrome 时也无法正常工作。当我尝试:

webView.loadDataWithBaseURL(...) 

如果我放在标题上它会起作用:

"<link rel='stylesheet' type='text/css' href='file:///android_asset/style.css'/>"

在我的 style.css 中:

@font-face {
    font-family: Andika;
    src: url('fonts/Andika.ttf');
}
@font-face {
    font-family: Calibri;
    src: url('fonts/Calibri.ttf');
}
@font-face {
    font-family: Century Gothic;
    src: url('fonts/CenturyGothic.ttf');
}
@font-face {
    font-family: Comic Sans MS;
    src: url('fonts/ComicSansMS.ttf');
}

@font-face {
    font-family: Lexia;
    src: url('fonts/Lexia.ttf');
}
@font-face {
    font-family: Lucida Sans Unicode;
    src: url('fonts/LucidaSansUnicode.ttf');
}
@font-face {
    font-family: Tahoma;
    src: url('fonts/Tahoma.ttf');
}
@font-face {
    font-family: Tiresias;
    src: url('fonts/Tiresias.ttf');
}
@font-face {
    font-family: Trebuchet MS;
    src: url('fonts/TrebuchetMS.ttf');
}
@font-face {
    font-family: Verdana;
    src: url('fonts/Verdana.ttf');
}

但它是在本地数据上。

我想用维基百科这样的外部页面来实现。

当我在维基百科 html 页面上注入链接时,我收到以下错误消息:

Not allowed to load local resource: file:///android_asset/

如果我直接将@font-face 放在样式字段中,我也会遇到同样的错误。

有人有解决这个错误的想法吗?

请!

谢谢

【问题讨论】:

    标签: android html css webview fonts


    【解决方案1】:

    你可以这样做

    1. 使用任何 HTTP 客户端获取维基百科页面的 HTML
    2. 使用 JSoup 解析并在其中插入您的 &lt;link... 标签
    3. 在您的WebView 中使用loadDataWithBaseURL 加载它

    【讨论】:

    • 我尝试过,但我丢失了所有图像和链接......但它可以工作,但页面不起作用......
    • 哦,这是因为您更改了基本 URL,如果您可以在其他地方托管您的字体,那么这可能会起作用。
    • 我尝试在我的网站上托管 style.css 和字体,但还有其他错误:Font from origin 'http://maximelimone.fr' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource....
    • this
    • 不工作我有同样的错误。我相信...谢谢你
    猜你喜欢
    • 2021-01-04
    • 2015-03-05
    • 2012-06-02
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多