简体   繁体   中英

WKWebView increase font size without adding horizontal scroll

So, the problem is following: We have simple text, :

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text  NED test test Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text  NED test test Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text  NED test test 

And we use WKWebView for displaying it:

webView.loadHTMLString(text, baseURL: nil)

We will see the result like

在此处输入图像描述

As you see the font is quite small, so I want to increase it. If I try to do

    private func viewPortScript() -> WKUserScript {
        let viewPortScript = """
            var meta = document.createElement('meta');
            meta.setAttribute('name', 'viewport');
            meta.setAttribute('content', 'width=device-width');
            meta.setAttribute('initial-scale', '1.0');
            meta.setAttribute('maximum-scale', '1.0');
            meta.setAttribute('minimum-scale', '1.0');
            meta.setAttribute('user-scalable', 'no');
            document.getElementsByTagName('head')[0].appendChild(meta);
        """
        return WKUserScript(source: viewPortScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    }

or

document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='200%'

It will increase the font, but we will see horizontal scroll in this case

在此处输入图像描述

Is there way to increase font size but without horizontal scroll

Find the issue...

webView.loadHTMLString(text.replacingOccurrences(of: " ", with: " "), baseURL: nil) 

We need to remove nbsp https://en.wikipedia.org/wiki/Non-breaking_space

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM