gpt4 book ai didi

swift - 用图像计算 UIWebView 高度给出了错误的答案

转载 作者:行者123 更新时间:2023-11-30 14:18:59 25 4
gpt4 key购买 nike

我找到了如何根据其内容计算动态 uiwebview 高度。我的代码是:

  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?我只能将其转换为 float ,如你所见。

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

 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 方法。这是我的代码:

  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()


}

关于swift - 用图像计算 UIWebView 高度给出了错误的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com