如何在WKWebView中更改文本选择颜色? [英] How to change the text selection color in WKWebView?

查看:559
本文介绍了如何在WKWebView中更改文本选择颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改WKWebView中的文本选择颜色.我尝试了几乎所有针对UIWebView/WKWebView提出的解决方案.

I'm trying to change text selection color in WKWebView. I tried pretty much all the solutions proposed for UIWebView/WKWebView that I was able to find.

更改色调颜色无效.将CSS应用于::selection-webkit-tap-highlight-color标签无效.

Changing tint color does not work. Applying css for ::selection, -webkit-tap-highlight-color tags works neither.

它总是蓝色.可以更改吗?

It's always blue. Is it possible to change it?

推荐答案

从iOS 13开始,设置WKWebView的tintColor属性还会更改选择颜色(和插入符号颜色).

As of iOS 13, setting WKWebView's tintColor property also changes the selection color (and the caret color).

WKWebView *webView = ...
webView.tintColor = UIColor.redColor;

额外提示:如果您的应用程序支持暗模式,但由于某种原因WKWebView内容必须为亮模式,则可以强制包含WKWebView的整个视图控制器具有亮模式特征,或者可以执行以下操作:

Extra tip: if you have an app that has dark mode support but for some reason the WKWebView content must be light mode, you can either force the whole view controller containing the WKWebView to have the light mode trait or you can do:

if (@available(iOS 13.0, *)) {
    webView.tintColor = [webView.tintColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]];
}

这可确保在html中带有浅色"内容的情况下,选择颜色和插入符号颜色可见

this ensures that the selection and caret colors will be visible with "light" content in the html

这篇关于如何在WKWebView中更改文本选择颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆