计算带有图像的UIWebView高度会给出错误的答案 [英] calculating UIWebView height with images gives wrong answer

查看:239
本文介绍了计算带有图像的UIWebView高度会给出错误的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了如何根据其内容计算动态uiwebview高度.我的代码是:

I found how to calculate dynamic uiwebview height according to its content. My code is :

  println("webViweDidFinish started")

    var frame:CGRect = myWebView.frame

    frame.size.height = 1

    var fittingSize :CGSize = myWebView.sizeThatFits(CGSizeZero)

    frame.size = fittingSize

    myWebView.frame = frame


    var contentsizeHeight = webView.scrollView.contentSize.height

    var yCoordinateOfWebView = myWebView.frame.origin.y


    var contentHeight: CGFloat = webView.scrollView.contentSize.height;

    myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //

    UIView.animateWithDuration(0.5, animations: {
        self.heightConstraints.constant = fittingSize.height

        self.myWebView.layoutIfNeeded()

        self.myScrollContentView.layoutIfNeeded()

    })



    self.activityIndicator.stopAnimating()

如果我使用这种方法:

        var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")

    var heightInFloat = height.floatValue

如何将高度转换为CGFloat?如您所见,我只能将其转换为浮点型.

How to convert height to CGFloat? I can only convert it to float as you see.

问题是,当webview包含太多图像时,高度计算错误.相反,当没有图像高度时,计算正确.当我重新加载该函数时,它第二次计算正确(带有图像).这是我正在加载的字符串的内容:

The problem is that when webview has too many images the height is calculated wrong. Oppositely when there is no image height is calculated right. When I reload the function, the second time it calculates right (with images). Here is content of string that I am loading:

 class func formatContentText(inout text: NSString)-> NSString{

    text = text.stringByReplacingOccurrencesOfString("src=\"", withString: "src=\"http://dixinews.kz")
    var deviceWidth = "300"
    text = "<html><head><meta name = \"viewport\" content = \"user-scalable=no, width=" + deviceWidth + " , maximum-scale=1.0\"><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body>" + text + "</body></html>"

    return text
}

推荐答案

我在javascript中使用了方法.这是我的代码:

I used approach with javascript. Here is my code:

  func webViewDidFinishLoad(webView: UIWebView) {





    var frame:CGRect = myWebView.frame



    // javascript
    var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")

    var heightInFloat = height.floatValue // convert to float

    var heightINCGFloat = CGFloat(heightInFloat) convert to cgfloat

    frame.size.height = heightINCGFloat //set heigh

    myWebView.frame = frame // set frame

    myWebView.scrollView.contentSize.height = myWebView.frame.height

    var contentsizeHeight = webView.scrollView.contentSize.height

    var yCoordinateOfWebView = myWebView.frame.origin.y

    myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //

    UIView.animateWithDuration(0.5, animations: {
        self.heightConstraints.constant = heightINCGFloat

        self.myWebView.layoutIfNeeded()

        self.myScrollContentView.layoutIfNeeded()

    })

    self.activityIndicator.stopAnimating()


}

这篇关于计算带有图像的UIWebView高度会给出错误的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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