UIWebview有时缩放页面,有时不缩放

时间:2014-09-26 07:20:54

标签: ios objective-c iphone uiwebview

我有以下UIWebview

    webNieuws = [[UIWebView alloc]initWithFrame:CGRectMake(10, height,300, 400)];
        [webNieuws setScalesPageToFit:NO];
        [[webNieuws scrollView] setBounces: NO];
        webNieuws.delegate = self;
        NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                       "<head> \n"
                                       "<style type=\"text/css\"> \n"
                                       "body {font-family: \"%@\"; size=\"12\" COLOR:#111111; background-color:transparent;}\n"
                                       "</style> \n"
                                       "</head> \n"
                                       "<body>%@</body> \n"
                                       "</html>", @"MyriadPro-Regular",_hotnews.hot_content]; 
        NSLog(@"Hot news content is %@",_hotnews.hot_content);


        [webNieuws setBackgroundColor:[UIColor yellowColor]];
        [webNieuws setOpaque:NO];

        [webNieuws loadHTMLString:myDescriptionHTML baseURL:nil];
        [scrollView addSubview:webNieuws];


- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame2 = aWebView.frame;
    frame2.size.height = 1;
    frame2.origin.y = height;
    aWebView.frame = frame2;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame2.size = fittingSize;
    CGFloat webHeight = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight;"] floatValue];
    frame2.size.height =webHeight;

   aWebView.frame = frame2;
    NSLog(@"Height is %f",webHeight);


    float newHeight2 =  30 + webHeight;
    NSLog(@"new height header is %f",newHeight2);



    [scrollView setFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width,newHeight2 + 320)];
    scrollView.bounces = NO ;



}

相同的webview会给出不同的结果,如下所示。我知道我可以设置scalespageTofit。但后来我放松了我的字体。

enter image description here

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

请在webViewDidFinishLoad方法

中试用
NSString *javascriptCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.0;"];
[aWebView stringByEvaluatingJavaScriptFromString:javascriptCommand];

如果这不起作用,请检查webView's autoresing属性