【问题标题】:UIWebView scalesPageToFit does not work correctlyUIWebView scalesPageToFit 无法正常工作
【发布时间】:2013-10-29 23:35:52
【问题描述】:

我正在使用 UIPageViewController 和其中的多个 uiwebviews,我使用这个加载了我之前的所有 webviews:

- (void) createContentPages
{
    NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
    for (int i = 0; i < chapters.count; i++)
    {
        NSString *path;
        path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%@",[arrayDeCapitulos objectAtIndex:i],idioma] ofType:@"txt"];
        NSString *arbit;
        if (path) {
            arbit = [[NSString alloc]initWithContentsOfFile:arbitPath encoding:NSUTF8StringEncoding error:nil];
        }
        NSString *a;
        a=@"LTR";
        if ([idioma isEqualToString:@"h"]) {
            a=@"RTL";
        }
        int size=[[[NSUserDefaults standardUserDefaults]objectForKey:@"preferredSize"]integerValue];
        NSString *contentString=[NSString stringWithFormat:@"<html DIR='%@'><head>"
                                 "<style>"
                                 "*{ padding-bottom:5;font-size:%i;"
                                 "    font-family:'SBL Hebrew';max-width: %fpx;}"
                                 "</style>"
                                 "<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' />"
                                 "</head>"
                                 "<body>"

                                 "%@"

                                 "</BODY> </HTML>",a,size,self.view.bounds.size.width ,arbit];
        [pageStrings addObject:contentString];
    }
    pageContent = [[NSArray alloc] initWithArray:pageStrings];
}

一切正常,但有时一切都出错了……我已经尝试了一切,有人知道吗?

然后在我的其他类中加载 uiwebview

[webView loadHTMLString:dataObject
                baseURL:[NSURL URLWithString:@""]];
[webView setScalesPageToFit:NO];
webView.delegate=self;

-(void)webViewDidFinishLoad:(UIWebView *)webview
{
    CGSize contentSize = webView.scrollView.contentSize;
    CGSize viewSize = self.view.bounds.size;
    float rw = viewSize.width / contentSize.width;
    webView.scrollView.minimumZoomScale = rw;
    webView.scrollView.maximumZoomScale = rw;
    webView.scrollView.zoomScale = rw;
}

结果如下:

【问题讨论】:

    标签: ios objective-c uiwebview


    【解决方案1】:

    我知道这听起来很奇怪,但是通过将 html 的内容移动到 div 来完成这项工作。

    NSString *contentString=[NSString stringWithFormat:@"<html DIR='%@'><head>"
                     "<style>"
                     "*{ padding-bottom:5;font-size:%i;"
                     "    font-family:'SBL Hebrew'}"
    
                     "</style>"
    
                     "</head>"
                     "<body>"
                     "<div>%@</div>"
    
                     "</body> </HTML>",a,size,arbit];
    

    【讨论】:

      【解决方案2】:

      试试这个

      NSString *contentString=[NSString stringWithFormat:@"<html DIR='320px'><head>"
                   "<style>"
                   "*{ padding-bottom:5;font-size:%i;"
                   "    font-family:'SBL Hebrew'}"
      
                   "</style>"
      
                   "</head>"
                   "<body>"
                   "<div>%@</div>"
      
                   "</body> </HTML>",size,arbit];
      

      你可以设置 div 宽度:320px;和 html 代码的高度 100%。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-10
        • 2016-09-14
        • 1970-01-01
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        相关资源
        最近更新 更多