【问题标题】:UIWebView : Get Webview Content size?UIWebView:获取 Webview 内容大小?
【发布时间】:2017-11-24 12:14:23
【问题描述】:

我已经在 webview 中加载了 HTML。我想动态更新网页视图中加载的文本的字体大小(增加或减少) 与 Apple News Article 应用程序中的相同。但根据我的设计,我需要停止 web 视图滚动并需要根据文本更新它的高度。所以我需要获取文章的内容大小,但我没有得到正确的。有人可以帮忙吗?

【问题讨论】:

标签: ios uiwebview


【解决方案1】:

当字体更改时,将 Webview 高度更改为 1,而不是为 Webview 获得正确的 offsetHeight

webView1.frame.size.height = 1
let size : String = self.webView1.stringByEvaluatingJavaScript(from: "document.documentElement.offsetHeight")!

【讨论】:

    【解决方案2】:

    将以下代码行添加到您的webViewDidFinishLoad

            webView.layoutSubviews()
            webView.frame.size.height = 1
            webView.frame.size = webView.sizeThatFits(.zero)
            print("WebView Height : \(webView.scrollView.contentSize.height)")
            webViewHgtConst.constant = webView.scrollView.contentSize.height
            webView.scalesPageToFit = true
            webView.scrollView.isScrollEnabled = false
            webView.scrollView.maximumZoomScale = 1.0
            webView.scrollView.minimumZoomScale = 1.0
    

    别忘了添加UIWebViewDelegate 并将YOUR_WEBVIEW.delegate = self 添加到您的viewDidLoad

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      首先停止滚动:

               webView.scrollView.isScrollEnabled = false
               webView.scrollView.showsVerticalScrollIndicator = false
               webView.scrollView.showsHorizontalScrollIndicator = false
               webView.scrollView.bounces = false
               webView.loadHTMLString(dataHtmlString, baseURL: nil)
      

      制作webView高度约束的出口,并通过计算其高度给出其值:

           webviewHeightConst.constant  = (dataHtmlString.htmlAttributedString()?.height(withConstrainedWidth: yourwidthcont))!
      

      这个扩展计算高度

      extension String {
      
      func htmlAttributedString() -> NSAttributedString? {
          guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
          guard let html = try? NSMutableAttributedString(
              data: data,
              options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
              documentAttributes: nil) else { return nil }
      
          var found = false
      
          html.beginEditing()
          html.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, html.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in
              if (value != nil) {
                                  let oldFont = value as! UIFont
                                 let newFont = oldFont.withSize(16)
                  html.addAttribute(NSFontAttributeName, value: newFont, range: range)
                  found = true
              }
          }
      
          if !found{
              // No font was found - do something else?
          }
          html.endEditing()
      
          return html
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-05
        • 1970-01-01
        • 2011-11-10
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2013-12-10
        • 1970-01-01
        • 2013-07-26
        相关资源
        最近更新 更多