【问题标题】:UIWebview content according to webview frame根据 webview 框架的 UIWebview 内容
【发布时间】:2011-10-11 00:18:31
【问题描述】:

我想禁用UIWebview里面的滚动,需要根据UIWebview的宽高显示内容。如果内容足够大,可以在页面上显示,它应该像 UILabel 中那样在文本的末尾显示 ...

flipboard 应用程序在第一页上显示部分内容的地方做得很好,当我们捏缩放它时,更多的内容会根据高度和宽度显示在视图上。

如何实现。对此的任何帮助表示赞赏。

示例:

我正在 iPad 上加载这个

UIWebView *mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(100, 100, 500, 730)];
mWebView.backgroundColor=[UIColor greenColor];
mWebView.userInteractionEnabled=YES;
mWebView.autoresizingMask=YES;
mWebView.delegate = self;
for (id subview in mWebView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;
for (id subview in mWebView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).showsVerticalScrollIndicator = NO;
for (id subview in mWebView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).scrollEnabled= NO;
[self.view addSubview:mWebView];
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mobile.washingtonpost.com/c.jsp;jsessionid=E6B119C6EF5A6274030E7B3A9F97D374?item=http%3a%2f%2fwww.washingtonpost.com%2fpolitics%2fobama-gop-leaders-said-to-discuss-new-debt-plan%2f2011%2f07%2f21%2fgIQAT81BSI_mobile.mobile&cid=578815"]]];
[mWebView release];

我只想显示可以在帧 500,750 上显示的那么多内容。正如您在屏幕截图中看到的,最后一行被剪掉了。

【问题讨论】:

    标签: javascript iphone html ios ipad


    【解决方案1】:

    禁用 UIWebView 的滚动:

    mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(20, 0, 300, 390)];
    mWebView.backgroundColor=[UIColor greenColor];
    mWebView.userInteractionEnabled=YES;
    mWebView.autoresizingMask=YES;
    mWebView.delegate = self;
    for (id subview in mWebView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;
    for (id subview in mWebView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).showsVerticalScrollIndicator = NO;
    for (id subview in mWebView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).scrollEnabled= NO;
    

    然后将此 UIWebView 添加为您的主 UIView 或任何您想要的子视图。

    每当重新加载数据时,都会调用 UIWebView 的以下委托方法。所以在里面实现动态调整大小的代码。

    - (void)webViewDidFinishLoad:(UIWebView *)webview{
       float webHeight;
       CGRect frame = webview.frame;
       frame.size.height = 1;
       webview.frame = frame;
       CGSize fittingSize = [webview sizeThatFits:CGSizeZero];
       frame.size = fittingSize;
       webview.frame = frame;
       webHeight=fittingSize.height;
       rowHt=webHeight;
       [aTableView reloadData];
    }
    

    我在 tableview 中添加了 UIWebView。所以我正在调用 tableview 的 reloadData 并将 webHeight(新的 webview 高度)传递给 table 委托以设置行高。希望这会有所帮助。

    【讨论】:

    • iPhoneFreak,谢谢你的意见。但我无法更改我的视图框架以适应内容。例如,假设适合内容的大小是 800,我想显示高度为 400,如果我显示它,最后一行中的文本将被垂直剪切。相反,我想在最后一行显示一个 ... ,就像在 Flipboard 中那样。希望你能理解。
    【解决方案2】:

    您可以使用 javascript 轻松完成。 在这里我可以给你一个想法休息是你必须实施。放置任何标签,让我们在您的 html 中的适当位置说 div 标签,并在此处放置一些信息,以便在用户单击加载下一个 div 时继续阅读,该 div 将具有另一个页面信息。就像我们真正想看的时候打开盒子一样。您可以使用 javascript 轻松完成。

    【讨论】:

      【解决方案3】:

      Flipboard 在呈现数据之前会重新格式化数据。要获得相同的效果,您需要做同样的事情。即读入 HTML,分离出组件并通过带有 HTML 和 CSS 的 UIWebview 或 UIKIT 以您自己的格式呈现。

      UIWebviews 本身不支持省略号溢出。如果你沿着 CSS 路线检查它支持省略号的溢出属性。

      【讨论】:

        【解决方案4】:

        你可以通过UIWebViewDelegate获取webView的高度。在方法中

        - (void)webViewDidFinishLoad:(UIWebView *)webView {
            height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
            webView.frame = CGRectMake(0,0,320,(height + 30));
        }
        

        然后使用该代码设置 webView 框架大小。

        【讨论】:

        • 西苏,谢谢您的回复。但我不想根据内容增加高度或宽度。相反,内容应该显示在 webview 框架中。问题是大多数情况下文本会在中间垂直剪切。
        • 抱歉,Anil Sivadas,不知道 iPad。但 webView 不会像 UILabel (...) 那样显示。你可以尝试使用 UIScrollView。使用 (100, 100, 500, 750) 设置滚动视图帧大小,并将 webView 添加为滚动视图 subview
        • 西苏,谢谢你的建议。滚动视图子视图不起作用。
        • UIWebView 添加到 UIScrollView 后,你得到了同样的结果?你设置了滚动视图的内容大小吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 2014-03-16
        • 2012-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多