如何在 web 视图中显示外国字体?

程序需要在一个 web 视图的 HtmlString 中显示外国字体

HtmlString ="<html><body style='background-color:#000000; color:ffffff; font-family:georgia,times new roman,times,seri,krutidev010; margin-top:0px; margin-bottom:0px; margin-right:0px; margin-left:0px; padding:0px;'><div style='padding:0px;'></div><div style='padding:5px;'><b>"+SharedVariables.ArrayOfNewsItems.get(SelectedRow).getTitle()+"</b><p align='justify'>"+SharedVariables.ArrayOfNewsItems.get(SelectedRow).getContent()+"</p></div><p></p></body></html>";
MyWebView.loadData(HtmlString, "contentType=text/html", "charset=UTF-8");

如何实现?

ChatGPT尝试为您解答,仅供参考
要在 HTML 中使用外国字体,您需要使用 @font-face 规则定义字体,并在 HTML 中使用这些字体。


首先,您需要下载所需的字体文件。您可以在网上找到免费的字体文件,也可以购买商业字体。然后,将字体文件上传到您的 Web 服务器上,或将其保存在本地。

然后,您需要在 HTML 中使用 @font-face 规则定义字体。例如,如果您想要使用 Georgia 字体,则可以使用以下代码:

@font-face {
  font-family: 'Georgia';
  src: url('fonts/Georgia.ttf') format('truetype');
}

将此代码放在您的 HTML 代码的头部,或者在单独的 CSS 文件中。


然后,您就可以在 HTML 中使用 Georgia 字体了。例如,您可以使用以下代码:

<div style="font-family: 'Georgia', serif;">
  This text will be displayed in Georgia.
</div>

最后,您可以在您的 HTML 代码中使用多种字体。例如,您可以使用以下代码:

@font-face {
  font-family: 'Georgia';
  src: url('fonts/Georgia.ttf') format('truetype');
}

@font-face {
  font-family: 'Times New Roman';
  src: url('fonts/TimesNewRoman.ttf') format('truetype');
}

@font-face {
  font-family: 'Kruti Dev';
  src: url('fonts/KrutiDev.ttf') format('truetype');
}

然后,您可以使用以下代码来在 HTML 中使用多种字体:

<body style='font-family: Georgia, "Times New Roman", serif, Kruti Dev;'>
  <div>
    This text will be displayed in Georgia.
  </div>
  <div>
    This text will be displayed in Times New Roman.
  </div>
  <div>
    This text will be displayed in Kruti Dev.
  </div>
</body>

请注意,在上面的示例中,我们使用了多个字体名称,并使用了逗号来分隔它们。浏览器将按照顺序使用每个字体,直到找到可用的字体为止。这是为了确保在某些浏览器中使用字体时不会出现问题。


此外,您还可以使用 CSS 中的 font-family 属性来指定字体。例如,您可以使用以下代码:

body {
  font-family: Georgia, "Times New Roman", serif, Kruti Dev;
}

然后,您可以在 HTML 中使用以下代码:

<body>
  <div>
    This text will be displayed in Georgia.
  </div>
  <div>
    This text will be displayed in Times New Roman.
  </div>
  <div>
    This text will be displayed in Kruti Dev.
  </div>
</body>