2

I am trying to let the user resize the text of the given web page. I am using Swift in Xcode 6. HTML file:

<!DOCTYPE html>
    <html>
    <body>

        <h1>My First Heading</h1>

        My first paragraph.

    </body>
</html>

and here is the swift function I am calling to try to change the size:

func changeWebViewFontSize(decOrInc: Int, webView: UIWebView)
{
//1 = decreace
//2 = increace
var textFontSizeTemp = defaults.objectForKey("textFontSize") as Int

switch decOrInc
{
case 1: //when decrease
    defaults.setObject(textFontSizeTemp - 1, forKey: "textFontSize")
case 2: //when increase
    defaults.setObject(textFontSizeTemp + 50, forKey: "textFontSize")
default:
    break
}

var jsString = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust=\(textFontSize)"
//var jsString = "alert('test')"
webView.stringByEvaluatingJavaScriptFromString(jsString)
}

I know that the javascript is being run because when I ran an alert() function in the javascript, it worked. Please help me find out what is wrong with my code!

MrWhetherMan
  • 1,184
  • 2
  • 11
  • 27
  • you are first reading the value of textFontSizeTemp in textFontSizeTemp = defaults.objectForKey("textFontSize") as Int, then increasing the size of it later in your switch case statement. Eventually using the value you read before update to set the font size, are you sure that's how you want to do it? – Amir Jan 30 '15 at 22:28
  • No... I was just trying to figure out how to get it done. I would be more than happy if you could suggest a different way to do it. – MrWhetherMan Jan 30 '15 at 22:38
  • And just wanted to check, you just want to increase the font size right? – Amir Jan 30 '15 at 22:40
  • check my anwser here https://stackoverflow.com/questions/39638019/resizing-text-in-uiwebview-swift-3/47195648#47195648 – Heshan Sandeepa Nov 09 '17 at 06:54

1 Answers1

0

I wrote an example like this which does the font resizing (increasing the size on tapping a button):

class webViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!

@IBOutlet weak var increase: UIButton!

var defaults  = ["textFontSize":12]

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

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://stackoverflow.com/questions/28245483/resizing-uiwebview-text/28245982?noredirect=1#comment44873831_28245982")


    let urlRequest = NSURLRequest(URL: url!)

    webView.loadRequest(urlRequest)        

}

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


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

    defaults["textFontSize"] = textFontSizeTemp


    var jsString = "document.getElementsByTagName('body')[0].style.fontSize='\(textFontSizeTemp)px'"
    webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
}
Amir
  • 9,577
  • 11
  • 41
  • 58
  • My html is going to be a lot longer than the one I listed, and I am going to have a lot of them. I can't just make each one a string. – MrWhetherMan Jan 31 '15 at 23:50
  • Doesn't matter, that's just an example, I've now replace the string html with the url of this SO question – Amir Feb 01 '15 at 00:02
  • Also, I am storing the textSize in NSUserDefaults so it will remember the the text size after the app closes. Am I doing that the right way, or should I do something different? – MrWhetherMan Feb 01 '15 at 00:16
  • That's fine to save it on user defaults but this question is about "Resizing UIWebView text". If you have other questions please post them on a different thread. – Amir Feb 02 '15 at 17:27
  • http://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts – Amir Feb 02 '15 at 23:51