调整UIWebView中的文本大小 - Swift 3 [英] Resizing text in UIWebView - Swift 3
本文介绍了调整UIWebView中的文本大小 - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到问题,在 UIWebView
中调整文本大小, html
文件位于包中。我已经设法在 obj-c
中使用修改过的脚本,但是使用 swift
3没有变化文本大小,尽管每次单击按钮
时,最佳值会正确更改。这是代码 -
I having a problem, resizing text within a UIWebView
, the html
file is located within the bundle. I have managed to get a modified script to work within obj-c
, however using swift
3 there is no change to the text size, although the optimal value changes correctly at each click of the button
. Here is the code -
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.
}
}
任何指针都感激不尽 - 谢谢
Any pointers gratefully received - Thank you
推荐答案
由于这是一个Web视图,您可以使用font size属性包装内容,
As this is a web view, you can wrap content with "font size" attribute,
let content = "<html><body><p><font size=30>" + webContent + "</font></p></body></html>"
webView.loadHTMLString(content, baseURL: nil)
这里相应地改变大小
这篇关于调整UIWebView中的文本大小 - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文