WebView is not working correctly in IOS MAUI

Miranda mary jose 0 Reputation points
2024-03-08T16:51:07.1866667+00:00

Not able to adjust the Font size of Web view iIn MAUi IOS, Added the handler and addded minimum Font size but it is not reflecting.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,381 Reputation points Microsoft Vendor
    2024-03-11T07:24:18.3+00:00

    Hello,

    Please try adding js to adjust the Font size, and you can refer to the following code:

    Custom WebView for MAUI

    public class CustomWebView : WebView {
    }
    

    Xaml in Page

    <local:CustomWebView x:Name="MyWebView" Source="XXX" HeightRequest="XXX'  WidthRequest="XXX"> </local:CustomWebView>
    

    Handler and setting delegate

    Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
            {
                if (view is CustomWebView)
                {
                    CustomWebView customWebView = view as CustomWebView;
    #if IOS
                    try
                    {
                        handler.PlatformView.NavigationDelegate = new MyDelegate(customWebView);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error at WebViewHandler: " + ex.Message);
                    }
    #endif
                }
            });
    

    WkWebviewDelagete

    #if IOS
    
    
    class MyDelegate : WKNavigationDelegate
    {
       
        public MyDelegate()
        {
           
        }
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
    
    // triple the size
            NSString js = (NSString)"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='200%'";
    
           webView.EvaluateJavaScript(js, (r, e) =>
            {
    
    
           });
        }
    }
    
    
    #endif
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments