3

I want to download html content, then find out it's size (by calling javascript functions). After that I want to change UIWebView frame to that size. And html content should fill whole UIWebView.

I read documentation and similar questions here on stackoverflow. Nothing helped. I don't want to call reload method and download HTML from server twice. I get correct width and height of html, but after changing UIWebView frame size, html content doesn't fill the frame. (this code below is called in - (void)webViewDidFinishLoad:(UIWebView *)webView)

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *width = [f numberFromString:[self.mediaWebView stringByEvaluatingJavaScriptFromString:@"getContentWidth()"]];
NSNumber *height = [f numberFromString:[self.mediaWebView stringByEvaluatingJavaScriptFromString:@"getContentHeight()"]];

self.mediaWebView.frame = CGRectMake(BORDER_WIDTH, BORDER_WIDTH, _frameWidth, _frameHeight);
self.mediaWebView.bounds = CGRectMake(0, 0, _frameWidth, _frameHeight);

Init code:

self.mediaWebView = [[UIWebView alloc] initWithFrame:CGRectMake(BORDER_WIDTH, BORDER_WIDTH, _frameWidth, _frameHeight)];
[self.mediaWebView setBackgroundColor:[UIColor clearColor]];
[self.mediaWebView setOpaque:FALSE];
[self.mediaWebView setUserInteractionEnabled:TRUE];
self.mediaWebView.scrollView.scrollEnabled = FALSE;
self.mediaWebView.scrollView.bounces = FALSE;
self.mediaWebView.delegate = self;
self.mediaWebView.contentMode = UIViewContentModeRedraw;
self.mediaWebView.backgroundColor = [UIColor blackColor];


NSString *stringUrl = @""// some URL

NSURL *url = [NSURL URLWithString:stringUrl];
[self.mediaWebView loadHTMLString:[self.questionView.question.mediaHtml description] baseURL:url];

self.mediaWebView.scalesPageToFit = TRUE;

[self addSubview:self.mediaWebView];
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
kraag22
  • 3,340
  • 3
  • 29
  • 37
  • I found it out. It was enaught to change property scalesPageToFit to FALSE. HTML content didn't changed when I played with frame size of WebView. So I started with max size of webview, then get correct size from JS functions and at the end change frame size. Content never scaled(zoomed,...) and that was exactly what I wanted. – kraag22 Nov 19 '12 at 13:10

1 Answers1

2

As I described in comment below question. It was enaugh to set scalesPageToFit to FALSE

kraag22
  • 3,340
  • 3
  • 29
  • 37
  • 3
    Good point +1. You should however seriously consider using YES/NO instead of TRUE/FALSE. The functionally is the same, but in Objective C you are expected to use those values for BOOL. You can read here why: http://stackoverflow.com/questions/6420987/why-does-objective-c-use-yes-no-macro-convention-instead-of-true-false – EsbenB Aug 01 '13 at 09:49