调整UIWebView中的文本大小 - Swift 3

时间:2016-09-22 11:40:24

标签: ios uiwebview swift3

我遇到问题,在UIWebView内调整文本大小,html文件位于包中。我已设法在obj-c内修改脚本,但是使用swift 3时文字大小没有变化,尽管每次点击button时最佳值都会正确变化。这是代码 -

import UIKit

class ViewController: UIViewController {

@IBOutlet var resWebView: UIWebView!

@IBOutlet weak var increaseFont: UIBarButtonItem!

@IBOutlet weak var decreaseFont: UIBarButtonItem!


var defaults  = ["textFontSize":40]


@IBAction func fontButtonPressed(sender: UIBarButtonItem) {

    var textFontSize = defaults["textFontSize"]

    switch sender.tag
    {
    case 1 : //when decrease
        textFontSize  = textFontSize! - 10

    case 2 ://when increase
        textFontSize = textFontSize! + 50
    default:
        break
    }

    defaults["textFontSize"] = textFontSize

    print(textFontSize)

    var jsString = "document.getElementsByTagName('body')[0].style.fontSize='\(defaults["textFontSize"])px'"
    resWebView.stringByEvaluatingJavaScript(from: jsString)
}


override func viewDidLoad() {
    super.viewDidLoad()

    let resFilePath = Bundle.main.url(forResource: "ResilienceHandbook", withExtension: "html");

    let resRequest = URLRequest(url: resFilePath!);
    resWebView.loadRequest(resRequest);


    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}
感谢任何指针 - 谢谢

3 个答案:

答案 0 :(得分:8)

由于这是一个Web视图,您可以使用"字体大小"来包装内容。属性,

        let content = "<html><body><p><font size=30>" + webContent + "</font></p></body></html>"
        webView.loadHTMLString(content, baseURL: nil)

这里相应地改变大小

答案 1 :(得分:2)

这个问题在这里得到解答:

Resizing UIWebView text

然而,我将其转换为Swift3并在Xcode8上进行了测试。这是代码:

类ViewController:UIViewController {

@IBOutlet weak var webView: UIWebView!

var defaults  = ["textFontSize":12]

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "https://stackoverflow.com/questions/39638019/resizing-text-in-uiwebview-swift-3")


    let urlRequest = NSURLRequest(url: url! as URL)

    webView.loadRequest(urlRequest as URLRequest)

}

func changeWebViewFontSize(zoomInOrZoomOut: Int, webView: UIWebView)
{
    //1 = decreace
    //2 = increace
    var textFontSizeTemp = defaults["textFontSize"]! as Int


    switch zoomInOrZoomOut
    {
    case 1: //when decrease
        textFontSizeTemp  = textFontSizeTemp - 20
    case 2: //when increase
        textFontSizeTemp = textFontSizeTemp + 20
    default:
        break
    }

    defaults["textFontSize"] = textFontSizeTemp


    let jsString = "document.getElementsByTagName('body')[0].style.fontSize='\(textFontSizeTemp)px'"
    webView.stringByEvaluatingJavaScript(from: jsString)
}

//UIButton Action
@IBAction func zoomOutButton_TouchUpInside(_ sender: AnyObject)
{
    changeWebViewFontSize(zoomInOrZoomOut: 1,webView: webView)
}

@IBAction func zoomInButton_TouchUpInside(sender: AnyObject)
{
    changeWebViewFontSize(zoomInOrZoomOut: 2,webView: webView)
}

答案 2 :(得分:1)

创建一个代码段变量,如下所示:

                           let snippet = "<html><head><style>body { font-family: -apple-system, GothamNarrowSSm-Medium; sans-serif;}</style><meta name=\"viewport\" content=\"width=device-width, initial-scale=0.5\"></head><body>\(YOUR_HTML_HERE)</body></html>"

将您的string / html变量放入看到YOUR_HTML_HERE的位置。

设置/替换为您喜欢的字体和缩放比例,以使用initial-scale = value调整大小。

然后,将其传递给WKWebView。

yourWebView.loadHTMLString(代码段,baseURL:无)

您都准备好了。