I am using a webview
custom renderer for showing the HTML data on UI. The default size of the webview content is too small.
My Code:
MyWebView.cs
public class MyWebView : WebView
{
public static readonly BindableProperty UrlProperty = BindableProperty.Create(
propertyName: "Url",
returnType: typeof(string),
declaringType: typeof(MyWebView),
defaultValue: default(string));
public string Url
{
get { return (string)GetValue(UrlProperty); }
set { SetValue(UrlProperty, value); }
}
}
MyWebViewRenderer.cs in ios
public class MyWebViewRenderer : ViewRenderer<MyWebView, WKWebView>
{
WKWebView _wkWebView;
protected override void OnElementChanged(ElementChangedEventArgs<MyWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var config = new WKWebViewConfiguration();
_wkWebView = new WKWebView(Frame, config);
SetNativeControl(_wkWebView);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == "Url")
{
Control.LoadHtmlString(Element.Url, null);
}
}
}
XAML and XAML.cs
<local:MyWebView
x:Name="web_view"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
</local:MyWebView>
web_view.Url = "htmldata";
Output Screenshot
The text and video sizes are very small, I don't see any option for increasing the font size of webview, please suggest a solutionn for this issue.
You can add
NavigationDelegate
for WKWebView to modify font size of webview .As follow:
The default effect :
The 200% effect:
=================================Update==================================
If want each element of
Html
to be reszied , you can add a header style before the contentHtml
.As follow :
For example , you can modify the value of
initial-scale
inside theheaderString
.Follow is the sample effect :initial-scale=1.0 effect :
initial-scale=1.5 effect :
=================================Update==================================
If want the width of Video fit the screen , I suggest that dealing this from
Html
soucre code . Because there areCSS
styles forHtml
to use .For example , if addingstyle='width:100%;'
for Video in Html , the Video will fit the width of screen as follow :The full code of Html :
==============================Update================================
If unable to modify code of
Html
, also can useWKNavigationDelegate
to getvideo
object from html to modify value for them .As follow :