我有一个问题,需要在UIWebView
中调整文本大小,html
文件位于bundle中。我已经成功地在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.
}
}
任何指针都将不胜感激 - 谢谢。