【发布时间】:2019-05-27 19:34:39
【问题描述】:
我正在开发跨平台应用程序。在 webview 我想使用@font-face。它在 Android 上运行良好,但在 UWP 上无法运行。我的字体文件位于 Assets/Fonts 文件夹和样式表中,我使用以下代码:
@font-face {
font-family: 'defont';
src: url('Assets/Fonts/mher.ttf');
}
在 Android 中我使用的是这个:
src: url('file:///android_asset/mher.ttf');
但是当我在 UWP 中尝试相同的方法时,它不起作用。 请问有人知道吗?
更新:
下面是我使用普通 WebView 的 C# 代码。我正在从本地 html 文件中获取 html 内容,替换一些占位符并将新的 html 字符串显示到 WebView。
var browser = new WebView();
var htmlSource = new HtmlWebViewSource();
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(WebViewPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("MyApp.Assets.MyTemplate.html");
string templateHtml = "";
using (var reader = new StreamReader(stream))
{
templateHtml = reader.ReadToEnd();
}
templateHtml = templateHtml.Replace("{pageHeading}", _post.Title);
templateHtml = templateHtml.Replace("{body}", _post.Content);
if (_post.IsFeaturedImageAvailable)
{
templateHtml = templateHtml.Replace("{imgSrc}", _post.FeaturedImageUrl);
}else
{
templateHtml = templateHtml.Replace("{imgSrc}", "");
}
templateHtml = templateHtml.Replace("pdfprnt-buttons", "pdfprnt-buttons hide");
htmlSource.Html = templateHtml;
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
browser.Source = htmlSource;
Content = browser;
【问题讨论】:
-
这个链接只是告诉你如何设计一个 html 页面。样式表已经工作正常,我的 html 页面正在从样式表中获取样式。唯一的问题是它没有得到@font-face。
-
此链接用于在 xamarin 表单控件(如标签等)中使用自定义字体。我已经在标签上使用此字体,并且工作正常。问题是在使用 css @fone-face 属性的 html 页面上的 WebView 中使用。
-
你读到最后了吗?