【问题标题】:set font face in webview from asset folder从资产文件夹在 webview 中设置字体
【发布时间】:2015-05-21 02:42:23
【问题描述】:

大家好,在我的应用程序中,我正在使用 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>";

但我认为它不起作用,因为它生成与 for 相同的输出:

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 但仍然会产生相同的输出。

然后我如何设置我的资产/字体文件夹中存在的字体类型 Roboto-Light.ttf。

任何建议将不胜感激。提前致谢

【问题讨论】:

  • 您是否尝试过将字体设置为视图而不是文本?

标签: android fonts android-fonts


【解决方案1】:

您必须扩展您的 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;
        }
    }
}

【讨论】:

    【解决方案2】:

    试试这个方法:

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

    【讨论】:

    • 我尝试了您的解决方案,但仍然无法正常工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多