简体   繁体   English

如何根据宽度调整UIWebView的所有内容的大小?

[英]How to resize all content of UIWebView according to width?

I am making an app in which I am getting some html data from web-service and I am showing that data in UIWebView. 我正在制作一个从Web服务中获取一些html数据的应用程序,并且正在UIWebView中显示该数据。 I have fixed the width of UIWebView "310" but some time data is not come in this frame and data is cut out from edges. 我已经固定了UIWebView“ 310”的宽度,但是有些时候该帧中没有数据,并且从边缘切出了数据。 I am receiving this data from web - 我正在从网上收到这些数据-

NSString *resul=@" <div align=\\"left\\">As stated Please give a&nbsp;chance&nbsp;to serve you.<br></div><div align=\\"left\\"><br></div><div align=\\"left\\"><div class=\\"lc\\" style=\\"margin: 0px; font-size: 11px; font-family: Arial, Helvetica, sans; clear: left; float: left; NSString * resul = @“ <div align=\\"left\\">As stated Please give a&nbsp;chance&nbsp;to serve you.<br></div><div align=\\"left\\"><br></div><div align=\\"left\\"><div class=\\"lc\\" style=\\"margin: 0px; font-size: 11px; font-family: Arial, Helvetica, sans; clear: left; float: left; <div align=\\"left\\">As stated Please give a&nbsp;chance&nbsp;to serve you.<br></div><div align=\\"left\\"><br></div><div align=\\"left\\"><div class=\\"lc\\" style=\\"margin: 0px; font-size: 11px; font-family: Arial, Helvetica, sans; clear: left; float: left; width: 348px; 宽度:348px; text-align: center;\\"><p style=\\"text-align: justify; line-height: 14px; margin: 0px 0px 14px; padding: 0px;\\"><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p></div><div class=\\"rc\\" style=\\"margin: 0px; font-size: 11px; font-family: Arial, Helvetica, sans; clear: right; float: right; width: 348px; 宽度:348px; text-align: center;\\"><h2 class=\\"why\\" style=\\"margin: 0px; padding: 0px; font-weight: normal; font-size: 12px; height: 26px; text-align: left; background-image: url(http://www.lipsum.com/images/en/heading.gif); background-position: 0px -52px; background-repeat: no-repeat no-repeat;\\"></h2><p style=\\"text-align: justify; line-height: 14px; margin: 0px 0px 14px; padding: 0px;\\">It is a long ehat a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p></div></div> text-align: center;\\"><h2 class=\\"why\\" style=\\"margin: 0px; padding: 0px; font-weight: normal; font-size: 12px; height: 26px; text-align: left; background-image: url(http://www.lipsum.com/images/en/heading.gif); background-position: 0px -52px; background-repeat: no-repeat no-repeat;\\"></h2><p style=\\"text-align: justify; line-height: 14px; margin: 0px 0px 14px; padding: 0px;\\">It is a long ehat a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p></div></div> " text-align: center;\\"><h2 class=\\"why\\" style=\\"margin: 0px; padding: 0px; font-weight: normal; font-size: 12px; height: 26px; text-align: left; background-image: url(http://www.lipsum.com/images/en/heading.gif); background-position: 0px -52px; background-repeat: no-repeat no-repeat;\\"></h2><p style=\\"text-align: justify; line-height: 14px; margin: 0px 0px 14px; padding: 0px;\\">It is a long ehat a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p></div></div>

I know the reason for that I am receiving the size 348 from web. 我知道我从网上收到348号尺寸的原因。 So I want to ask that how to resize it according to our frame size. 因此,我想问一下如何根据我们的框架尺寸调整尺寸。 I have used - 我用过 -

webview.autoresizingMask = UIViewAutoresizingFlexibleWidth;
webview.scalesPageToFit = YES;
webview.autoresizesSubviews=YES; 

but data is showing in so small size.![data is showing in show small][1] 但是数据显示的大小很小。![数据显示在show small] [1]

and I have also used delegate method webViewDidFinishLoad but not get proper formatting. 而且我还使用了委托方法webViewDidFinishLoad,但未获得正确的格式。

You can edit the "resul" String.... I mean just change the "width: 348px;" 您可以编辑“ resul”字符串。...我的意思是只需更改“ width:348px;” to "width: 310px;" 改为“宽度:310像素;”

Try this: 尝试这个:

NSString *str1 = [[NSString alloc] initWithFormat:@"width: 348px;"];
NSString *str2 = [[NSString alloc] initWithFormat:@"width: 310px;"];
NSRange indexrange = [resul rangeOfString:str1];
while (indexrange.length !=0) {
    [resul replaceCharactersInRange:indexrange withString:str2];
    indexrange = [resul rangeOfString:str1];
}

I had a similar problem. 我有一个类似的问题。 This was my solution (it also covers orientation change): 这是我的解决方案(它还包括方向更改):

   float portraitRatio;

in - (void)webViewDidFinishLoad:(UIWebView*)theWebView add: 在-(void)webViewDidFinishLoad:(UIWebView *)theWebView中添加:

 UIScrollView *scroll = theWebView.scrollView;
 scroll.scrollEnabled = YES;
 portraitRatio = theWebView.bounds.size.width/scroll.contentSize.width;
 [self resizeOnOrientationChanged];


- (void) resizeOnOrientationChanged
{
float zoom;
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
    //iPhone
    for (UIWebView *aWebView in openWebViewsArray) {

        [aWebView setFrame:CGRectMake(0, 30, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-30)];
        if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
            zoom = aWebView.bounds.size.width/[webView scrollView].contentSize.width;
        }
        else {
            zoom = portraitRatio;
        }
        NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = %f;",zoom];
        [aWebView stringByEvaluatingJavaScriptFromString:jsCommand];
    }
    [self.webView setFrame:CGRectMake(0, 30, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-30)];
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        zoom = self.webView.bounds.size.width/[self.webView scrollView].contentSize.width;
    }
    else {
        zoom = portraitRatio;
    }
    NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = %f;",zoom];
    [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
}
else {
    for (UIWebView *aWebView in openWebViewsArray) {

        [aWebView setFrame:CGRectMake(0, 60, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-60)];
        if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
            zoom = aWebView.bounds.size.width/[webView scrollView].contentSize.width;
        }
        else {
            zoom = portraitRatio;
        }
        NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = %f;",zoom];
        [aWebView stringByEvaluatingJavaScriptFromString:jsCommand];
    }
    [self.webView setFrame:CGRectMake(0, 60, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-60)];
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        zoom = self.webView.bounds.size.width/[self.webView scrollView].contentSize.width;
    }
    else {
        zoom = portraitRatio;
    }
    NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = %f;",zoom];
    [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM