Xamarin Forms 4.5 introduced a new way to add Custom font (External font) as an Embedded Resource in our project, so that Font file needs to be added only in the shared project and not in every project such as Android iOS and UWP. Please note that I am using this approach.
Now coming to the question, I want to use Custom Font as an Embedded Resource in a WebView in Xamarin Forms. How to do it. I have done all the prerequisites for it, like Set Build Action - EmbeddedResource, added ExportFont Code as specified here. Also please note that I can use Custom font on a Label and Button in my Page. I just want to use the same Embedded Font in a WebView.
Here is my XAML code:
<WebView x:Name="SampleWebView" HeightRequest="400" />
The Code Behind C#:
private void RenderTextOnWebView()
{
HtmlWebViewSource htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<html>
<head>
<style>
@font-face {
font-family: myFirstFont;
src: url(MyApplication/Fonts/MonotypeCorsiva.ttf);
}
div {
font-family: myFirstFont;
}
</style>
</head>
<body>
<h1>The font Example</h1>
<div>With CSS, websites can finally use fonts other than the pre selected web - safe fonts.</div>
</body>
</html>";
SampleWebView.Source = htmlSource;
}