从资产文件夹中设置webview中的字体

时间:2015-03-18 10:46:18

标签: android fonts android-fonts

大家都在我的应用程序中我喜欢webview,在这个webview中我想以编程方式设置字体类型。目前在我的资产文件夹中,我有Roboto-Bold.ttf,Roboto-Light.ttf,.....和其他一些文件。目前我将其设置为:

text = "<html><head>"
                + "<style type=\"text/css\">body{color: #000; text-align: justify; background-color: #fff;font-family: file:///android_asset/fonts/Roboto-Medium;}"
                + "</style></head>"
                + "<body>"
                + sb.toString()
                + "</body></html>";

但我认为它不起作用,因为它产生的输出与:

相同
text = "<html><head>"
                + "<style type=\"text/css\">body{color: #000; text-align: justify; background-color: #fff;font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;}"
                + "</style></head>"
                + "<body>"
                + sb.toString()
                + "</body></html>";

这里我使用的是font-familyas:font-family:\“Helvetica Neue但仍然产生相同的输出。

然后我如何设置我的assets / fonts文件夹中的字体类型Roboto-Light.ttf。

任何建议将不胜感激。在此先感谢

2 个答案:

答案 0 :(得分:0)

尝试这种方式:

text = "<html><head>"

            + "<style type=\"text/css\"> @font-face { font-family:'HelveticaNeue'; src: url('file:///android_asset/HelveticaNeue.TTF');}body{color: #000; text-align: justify; background-color: #fff;font-family: \"HelveticaNeue\";}"

            + "</style></head>"

            + "<body>"

            + sb.toString()

            + "</body></html>";

答案 1 :(得分:0)

您必须扩展您的webview,创建新的WebViewClient,然后覆盖shouldInterceptRequest并查找任何字体请求,如下所示:

text = "<html><head>"

        + "<style type=\"text/css\"> @font-face { font-family:'Roboto'; src: url('Roboto-Light.ttf') format('truetype');}body{color: #000; text-align: justify; background-color: #fff;font-family: \"Roboto\";}"

        + "</style></head>"

        + "<body>"

        + sb.toString()

        + "</body></html>";

android的WebView类看起来应该是这样的:

public class Temp extends WebView
{
    Context mContext;

    public Temp(Context context)
    {
        this(context, null);
    }

    public Temp(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        setWebViewClient(createWebViewClient());
        setWebChromeClient(new WebChromeClient());
    }

    protected WebViewClient createWebViewClient()
    {
        return new WebViewClient()
        {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url)
            {
                if(url.contains("Roboto-Light.ttf"))
                {
                    return getFontFromAsset("Roboto-Light.ttf", "application/x-font-ttf");
                }
                else
                {
                    return null; // let the webview handle request
                }
            }
        };
    }

    private WebResourceResponse getFontFromAsset(String fontName, String mimeType)
    {
        InputStream fontIn = null;
        try
        {
            fontIn = mContext.getAssets().open(fontName);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        if(fontIn == null)
            return null;
        else
        {
            WebResourceResponse response = new WebResourceResponse(mimeType, "UTF-8", fontIn);
            return response;
        }
    }
}