Javascript in UIWebView
I'm using UIWebView to display article and one of functionality requirements is text resizing. Since, setting increased size and reloading UIWebView loses scroll offset I decided use Javascript solution.
Article HTML body tag looks like this: <body style='font-size:12px;'>
And resizing function basically looks like this:
NSString * str;
str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].styl开发者_开发问答e.fontSize=%d;", app.storyTextSize];
[textView stringByEvaluatingJavaScriptFromString:str];
[textView stringByEvaluatingJavaScriptFromString:str];
And as a matter of fact it works. In my iPod it works great (double stringByEvaluatingJavascript does the trick), but I have reports from other tester that in his iPhone sometimes it works and sometimes it doesn't (I guess what he meant by that is that not every button click executes Javascript properly even if it should be executed twice for reassurance).
Is there any way I could optimize my solution and make it more bullet proof? Or maybe there is a way to scroll back to previous position after reloading UIWebView, but, once again, setting offset in UIWebView will be Javascript bussiness..
Isn't the "sometimes it works, sometimes not" linked to the fact that sometimes the javascript is invoked before the HTML page is fully loaded, sometimes after the full load ?
精彩评论