ios - Resizing text in UIWebView - Swift 3 -
i having problem, resizing text within uiwebview
, html
file located within bundle. have managed modified script work within obj-c
, using swift
3 there no change text size, although optimal value changes correctly @ each click of button
. here 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); // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
any pointers gratefully received - thank you
this question answered here:
however converted in swift3 , tested on xcode8. here code:
class 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! url) webview.loadrequest(urlrequest urlrequest) } func changewebviewfontsize(zoominorzoomout: int, webview: uiwebview) { //1 = decreace //2 = increace var textfontsizetemp = defaults["textfontsize"]! 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) }
Comments
Post a Comment