我想下载 html 内容,然后找出它的大小(通过调用 javascript 函数)。之后,我想将UIWebView
框架更改为该大小。并且 html 内容应该填满整个UIWebView
.
我在 stackoverflow 上阅读了文档和类似的问题。没有任何帮助。我不想调用reload方法并从服务器下载 HTML两次。我得到正确的 html 宽度和高度,但是在更改UIWebView
框架大小后,html 内容不会填满框架。(下面的这段代码被调用- (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);
初始化代码:
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];