【问题标题】:How to activate darkmode for the text changed in WkWebViewRenderer?如何为 WkWebViewRenderer 中更改的文本激活暗模式?
【发布时间】:2021-05-24 22:55:43
【问题描述】:

我试图在 Xamarin.iOS 中使用 webView-renderer 调整 webView 中的文本大小。它有效,但我的问题是暗模式不再有效。我已经更改了 web 视图的背景,所以它显示正确,但我不知道如何处理文本。

  public class NavigationDelegat : WKNavigationDelegate
{
    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        string Size = "300%"; 
        string text = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", Size);
        
        WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
            if (err != null)
            {
                System.Console.WriteLine(err);
            }
            if (result != null)
            {
                System.Console.WriteLine(result);
            }
        };

        webView.EvaluateJavaScript(text, handler);
        webView.Opaque = false;
        webView.BackgroundColor = UIColor.Clear;


    }
}

怎么做?文本始终为黑色,激活 darkMode 时也是如此。

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios custom-renderer


    【解决方案1】:

    你可以试试下面的代码:

    public class NavigationDelegat : WKNavigationDelegate
    {
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            string Size = "300%"; 
            string textSize = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", Size);
            string TextColor = "#ffffff"; 
            string textColor = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextFillColor = '{0}'", TextColor);
            
            WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
                if (err != null)
                {
                    System.Console.WriteLine(err);
                }
                if (result != null)
                {
                    System.Console.WriteLine(result);
                }
            };
    
            webView.EvaluateJavaScript(textSize , handler);
            webView.EvaluateJavaScript(textColor, handler);
            webView.Opaque = false;
            webView.BackgroundColor = UIColor.Clear;
        }
    }
    

    =============================更新=========== ==================

    private class WKwebviewdeleagte : WKNavigationDelegate
    {
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            base.DidFinishNavigation(webView, navigation);
            if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                string Size = "300%";
                string TextSize = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", Size);
                string TextColor;
                string BackgroundColor;
                if (UITraitCollection.CurrentTraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Light)
                {
                    string textColor = "#666666";
                    TextColor = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextFillColor = '{0}'", textColor);
                    string backgroundColor = "\"#FFFFFF\"";
                    BackgroundColor = String.Format(@"document.body.style.backgroundColor='{0}'", backgroundColor);
                }
                else
                {
                    string textColor = "#ffffff";
                    TextColor = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextFillColor = '{0}'", textColor);
                    string backgroundColor = "\"#001A1A\"";
                    BackgroundColor = String.Format(@"document.body.style.backgroundColor='{0}'", backgroundColor);
                }
    
                WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
                    if (err != null)
                    {
                        System.Console.WriteLine(err);
                    }
                    if (result != null)
                    {
                        System.Console.WriteLine(result);
                    }
                };
    
                webView.EvaluateJavaScript(TextSize, handler);
                webView.EvaluateJavaScript(TextColor, handler);
                webView.EvaluateJavaScript(BackgroundColor, handler);
                //webView.Opaque = false;
                //webView.BackgroundColor = UIColor.Clear;
            }
        }
    }
    

    【讨论】:

    • 谢谢,但这会使文本始终为白色。我希望它在暗模式下为白色,在正常模式下为黑色。
    • @M_B 您可以检测到UITraitCollection.CurrentTraitCollection.UserInterfaceStyle 来修改背景中的文本颜色或背景颜色。我已经更新了答案。
    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2021-07-19
    • 2014-05-08
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多