UIWebView调整大小以适应内容 [英] UIWebView resize to fit Content

查看:134
本文介绍了UIWebView调整大小以适应内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我要做的事情,

我有一个webView我的框架设置为

I have a webView whose frame I am setting as

self.webView.frame = CGRectMake(0, Y,screenWidth,1);

y坐标是根据动态添加的其他视图的高度计算得出的。

the y coordinate is calculated based on the height of the other views that are added dynamically which works fine.

screenWidth设置为320或480,具体取决于设备方向,也可正常工作。

the screenWidth is set to 320 or 480 depending on device orientation which also works fine.

现在我想要我的webView将其宽度设置为screenWidth并仅调整其高度以适应内容。

Now I want my webView to set it's width as the screenWidth and resize it's height alone to fit the content.

内容可以是plainText,我加载为

The content could be plainText which I load as

        NSData* data=[msgBody dataUsingEncoding:NSUTF8StringEncoding];             
        if (data) 
        [self.webView loadData:data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:nil];  

或我加载的HTML

        [self.webView loadHTMLString:htmlMsgBody baseURL:nil]; 

在我的WebViewDidFinishLoad方法中,我这样做,

In my WebViewDidFinishLoad method, I am doing this,

- (void)webViewDidFinishLoad:(UIWebView *)webview {

    CGRect frame = self.webView.frame;
    CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    self.webView.frame = frame;

   CGFloat totalHeight = self.webView.frame.origin.y+self.webView.frame.origin.height+30;

    //the webView is scrolllocked and put on a scrollView so I do this

    [self.theScrollView setContentSize:CGSizeMake(screenWidth, totalHeight)];
}

我放置webView的scrollView可以水平滚动。无论如何,我的webView都是scrollLocked。所以我必须始终保持我的webView宽度与self.view.frame.size.width相同,这取决于设备方向,webView宽度应该只是改变以适应内容。因此我设置scrollView contentSize以允许垂直滚动。

My scrollView on which the webView is put can scroll horizontally. My webView is scrollLocked anyways. So I have to always keep my webView width to be the same as self.view.frame.size.width based on the device orientation and the webView width should change just to fit the content. I accordingly set the scrollView contentSize to allow vertical scrolling.

但问题是,self.webView sizeThatFits方法返回奇怪的值,当我的screenWidth为320时,我得到的fitSize.width为408和我的内容水平离开屏幕,我无法正确显示内容。即使我强行设置self.webView.frame.size.width = screenWidth的框架,部分内容仍然在屏幕外。

But the thing is that, the self.webView sizeThatFits method returns weird values and what happens is that when my screenWidth is 320 I get fittingSize.width as 408 and my content goes off screen horizontally and I am not able to show the content properly. Even if I forcibly set the frame of self.webView.frame.size.width = screenWidth, part of the content remains off screen.

如何处理这种情况?

推荐答案

根据文档,大小适合:


调整并移动接收器视图,使其仅包含 子视图

Resizes and moves the receiver view so it just encloses its subviews.

这不会向您提供有关将webView纳入其包含视图的任何信息。
您的webView是否可能尝试显示比screenWidth更宽的内容,并且由于您将其滚动锁定,它会显示整个宽度?

This does not give you any information about fitting the webView into it's containing view. Is it possible that your webView is trying to display something wider than the screenWidth, and since you have it scroll locked, it is showing the whole width?

这篇关于UIWebView调整大小以适应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆