打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android webview 使用自定义自体
2015-09-10 20:32 181人阅读 评论(1) 收藏 举报
分类:

原文:http://blog.isming.me/2015/07/07/Android-custom-font/

最近的一个项目中有个需求:app与webview需要统一使用自定义字体。

一:修改App的默认字体

1.把myfont.ttf放在assets/fonts 目录下

2.配置Calligraphy

dependencies {
    compile 'uk.co.chrisjenx:calligraphy:1.2.0'
}
  githut地址:https://github.com/chrisjenx/Calligraphy

2.在 BaseApplication extends Application 的onCreate 方法中

  1. CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()  
  2.                        .setDefaultFontPath("fonts/myfont.ttf")  
  3.                        .setFontAttrId(R.attr.fontPath)  
  4.                        .build()  
3.在BaseActivity 中重写attachBaseContext方法
  1. protected void attachBaseContext(Context newBase) {  
  2.        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));  
  3.    }  
二:修改webview里网页的字体:

1.对于本地的网页,在asset目录放字体文件,并在css中添加以下内容,自定义一个字体face,并且在需要的地方使用这个字体face即可。

  1. @font-face {  
  2.       font-family: 'MyCustomFont';  
  3.       src: url('file:///android_asset/fonts/myfont.ttf');   
  4.     }  
  5.     body{  
  6.       font-family:"MyCustomFont";  
  7.     }  

2.对于在线的网页,则需要把字体文件放到服务器,使用同样的方式定义字体face,应用到每个地方。

为了减少网页或者说服务器端的工作,可以使用本地注入的方式注入font-face的css,并对整个网页进行样式替换。

给webview自定义webViewClient,重写onPageFinish,在其中添加如下内容:

  1. public void onPageFinished(WebView view, String url) {  
  2.                 injectCSS();  
  3.                 view.loadUrl("javascript:!function(){" +  
  4.                         "s=document.createElement('style');s.innerHTML="  
  5.                         + "\"@font-face{font-family:myhyqh;src:url('****/fonts/myfont.ttf');}*{font-family:myhyqh !important;}\";"  
  6.                         + "document.getElementsByTagName('head')[0].appendChild(s);" +  
  7.                         "document.getElementsByTagName('body')[0].style.fontFamily = \"myhyqh\";}()");  
  8.                 super.onPageFinished(view, url);  
  9.             }  

  1. ****/fonts/myfont.ttf  //这里的* 号是为了让它走:shouldInterceptRequest  
重写vwebview的shouldInterceptRequest方法:

  1. public WebResourceResponse shouldInterceptRequest(WebView view,  
  2.                     String url) {  
  3.                 WebResourceResponse response = super.shouldInterceptRequest(view, url);  
  4.                 Log.i("webview","load intercept request:" + url);  
  5.                  if (url != null && url.contains("myfont.ttf")) {  
  6.                         String assertPath ="fonts/myfont.ttf";  
  7.                         try {  
  8.                             response = new WebResourceResponse("application/x-font-ttf","UTF8", getAssets().open(assertPath));  
  9.                         } catch (IOException e) {  
  10.                             e.printStackTrace();  
  11.                         }  
  12.                     }  
  13.                 return response;  
  14.             }  

这样网页被新字体重新渲染就达到目的了


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在网页中嵌入任意字体的解决方案
python爬虫反爬 | 对方是如何丧心病狂的通过 css 加密让你爬不到数据的
程序安装字体或直接调用非注册字体[c#]
什么是@font
c# winform里使用指定的字体
CSS 加载外部字体文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服