我遇到问题,在UIWebView
内调整文本大小,html
文件位于该包内。我已经设法在obj-c
之间获得修改的脚本,但是使用swift
3虽然每次单击button
时最佳值都会正确更改,但文本大小没有变化。下面是代码 -在UIWebView中调整文本大小 - Swift 3
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.
}
}
任何指针感激地接受 - 谢谢你
感谢您的回复rohit90,脚本作为我的原创,字体大小不会改变,尽管按钮在控制台中显示。有趣的是,当在你的脚本中使用URL时,我得到一个空白的白色屏幕(带有按钮),并且在增加大小时使用包HTML文件什么都没有发生(除了控制台日志记录)并且减小大小之间会在段落,但不会更改字体大小。 – Bowcaps
首先尝试一下我在代码中使用的URL,让我知道这是否可行,我认为你因为这个而变得空白屏幕http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a -cleartext-http – rohit90
是的,允许任意负载修复了白屏问题 - 谢谢。然而,文本不会调整大小,它似乎只是在文本行之间添加空行,但实际字体仍然是固定的 - 令人困惑? – Bowcaps