【问题标题】:Custom Font Typeface for CSS StyleCSS 样式的自定义字体字体
【发布时间】:2019-05-14 18:15:47
【问题描述】:

我的 Android 应用程序中有一个 WebView 来显示文本内容。 CSS 属性使用以下代码实现:

public static final String CSS_PROPERTIES = "<style>body{width:100%;margin:0;}
  img {max-width:100%;height:auto;} iframe{width:100%;}</style>";

我尝试将代码更改为以下语法,但没有任何帮助:

public static final String CSS_PROPERTIES = "<style type='text/css'>@font-face { font-family: nafees; src: url('file:///android_asset/fonts/nafees.ttf'); } 
  body p {font-family: nafees; width:100%;margin:0;}
  img {max-width:100%;height:auto;} iframe{width:100%;}</style>";

我希望将字体应用到 webView。

然后这个 CSS_PROPERTIES 字符串被实现为:

contentText = new StringBuilder().append(AppConstant.CSS_PROPERTIES)
.append(contentText).toString();

然后

webEngine.loadHtml(contentText);

【问题讨论】:

    标签: android html css xml


    【解决方案1】:

    请尝试以下代码。

    webView = findViewById(R.id.webview);
    
            String pish = "<html><head><style type=\"text/css\">@font-face {font-family: sanfranciscotext_bold;src: url(\"file:///android_asset/font/sanfranciscotext_bold.otf\")}body {font-family: sanfranciscotext_bold;font-size: medium;text-align: justify;}</style></head><body>";
            String pas = "</body></html>";
            String myHtmlString = pish + "Hello This is test for Font" + pas;
            webView.loadDataWithBaseURL(null,myHtmlString, "text/html", "UTF-8", null);
    

    像这样在 assets 文件夹中添加字体资源。

    输出如下:

    【讨论】:

    • 非常感谢!在您的帮助和支持下,我能够解决我的问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 2014-05-28
    • 2020-02-29
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多