Я написал код, чтобы получить высоту на основе document.body.offsetHeight не сработало, поскольку добавлялось пустое пространство в конце webView.
Поэтому я использовал следующий метод.
Код: Выделить всё
extension PDDescriptionCell: WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
self.summaryHeight?.constant = 0
self.contentView.layoutIfNeeded()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.summaryHeight?.constant = webView.scrollView.contentSize.height
if let tableView = self.superview as? UITableView {
tableView.beginUpdates()
tableView.endUpdates()
}
self.contentView.layoutIfNeeded()
}
}
Как мне убедиться, что высота строки 3 для PDescriptionCell обновлена, когда строка 3 находится на самом экране при таком способе определения высоты?
Полный код для PDescriptionCell:
Код: Выделить всё
class PDDescriptionCell: UITableViewCell {
var product: ProductModel? {
didSet {
if let product = product {
print(product.summary)
loadContent(product.summary)
}
}
}
IBOutlet weak var summaryView: WKWebView!
IBOutlet weak var summaryHeight: NSLayoutConstraint!
private let configuration = WKWebViewConfiguration()
override func awakeFromNib() {
super.awakeFromNib()
print("awake from nib")
contentView.backgroundColor = .background1
summaryView.navigationDelegate = self
let preference = WKWebpagePreferences()
preference.preferredContentMode = .mobile
preference.allowsContentJavaScript = true
let configuration = WKWebViewConfiguration()
configuration.defaultWebpagePreferences = preference
if let product = product {
_ = WKWebViewConfiguration()
print("HTML string is loaded")
self.summaryView.loadHTMLString(product.summary, baseURL: nil)
self.loadContent(product.summary)
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
private func loadContent(_ content: String) {
let styledHTML = """
body { font-size: 30px; /\* Change the font size for the body as needed \*/ font-family: -apple-system; /\* Use the system font \*/ } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; font-size: 20px; /\* Change the font size for table cells as needed \*/ } th { background-color: #f2f2f2; } img { width: 100%; /\* Set the width of images to 100% \*/ height: auto; /\* Set the height of images to auto to maintain aspect ratio \*/ margin-bottom: 50px; /\* Add space below each image \*/ } .semibold { font-weight: 600; /\* Set font weight to semibold \*/ }
window.onload = function() { var body = document.body; var html = document.documentElement; var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight); window.webkit.messageHandlers.iOS.postMessage(height); }
\(content)
"""
// Load the styled HTML content into the WKWebView
self.summaryView.loadHTMLString(styledHTML, baseURL: nil)
}
}
extension PDDescriptionCell: WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
self.summaryHeight?.constant = 0
self.contentView.layoutIfNeeded()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.summaryHeight?.constant = webView.scrollView.contentSize.height
if let tableView = self.superview as? UITableView {
tableView.beginUpdates()
tableView.endUpdates()
}
self.contentView.layoutIfNeeded()
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... -wkwebview
Мобильная версия