正在将图像从 HTML 加载到 UITextview/WKWebView
Loading Images into UITextview/WKWebView from HTML
正在尝试将包含图像的 HTML 内容加载到 UITextView 或 WKWebView 中(我只需要渲染它!)
但是,当我将 HTML 加载到 WKWebView 时,图像不会加载,视图也不会重新加载,并且遵守内容加载之前的自动布局约束:
相关代码在这里:
webView.navigationDelegate = self
webView.loadHTMLString(body, baseURL: nil)
和
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
guard let safeSelf = self else {return}
if let height = result as? CGFloat {
print("height of webview: \(height)")
webView.frame.size.height += height
//Layout subviews?
}
})
}
}
但是,根据 使用 UITextView,我可以执行以下操作:
extension String {
var htmlToAttributedString: NSMutableAttributedString? {
guard let data = data(using: .unicode) else { return NSMutableAttributedString() }
do {
return try NSMutableAttributedString(data: data, options: [.documentType: NSMutableAttributedString.DocumentType.html], documentAttributes: nil)
} catch {
return NSMutableAttributedString()
}
}
并将其设置为:
textView.attributedText = htmlString.htmlToAttributedString
这在自动布局方面效果很好...仅适用于文本,但它不保留图像(甚至不显示图像将去向的指示),仅保留文本。不保留 html.
使用的字体或字体大小
我直接使用了那个 post 中接受的答案,它不保留图像,即使 OP 要求保留图像。
什么是 a) 将内容加载到 WKWebView 并加载图像的正确方法(我的传输安全设置为允许此演示的任意加载)或 b) 在 UITextView 的解析中保留图像 HTML?我直接得到 html 而不是 URL.
编辑:出于测试目的,我在我的代码中硬编码了一个小样本 html 用于 UIWebView / WKWebView 以反映我从 API 调用中获得的部分内容。
let body = "<div class=\"media-attachment\"><a href=\"https://www.hnmagazine.com/2017/11/07/latinx-really-mean/\"><img src=\"https://testimages.weareher.com/feed/0/2018-08/t9Qc549F7uCtZdbdXx1ERLbXJN2EXIXVacouJDlX.png\"/></a><p class=\"media-caption\">via <a href=\"https://latinxontherise.podbean.com/\">Latinxontherise</a></p></div></div>"
我建议您应该使用 UIWebView 加载 HTML 字符串。记住为 webView 拖动委托,你只调用:your_webview.loadHTMLString(yourHTMLString, baseURL: nil)
your_webview.scalesPageToFit = 真
所以,图片没有加载,因为它们是 webP 图片,Apple 不支持 webP(因为它是 google 东西)。
至于自动布局的东西,这是基本的解决方案(对于 WKWebView):
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//A delay here is needed because didFinish does not guarantee content has finished displaying
//Height can only be retrieve after display finishes - hence the delay.
var height: CGFloat = 0.0
dispatch_after(0.1, block: {
height = webView.scrollView.contentSize.height
webView.snp.makeConstraints({make in //I used SnapKit here, because that's how my repo is setup
make.height.equalTo(height)
})
// If the the webview is in another view, update that view's frame with the height + the existing height of that view's frame. For me, I had the webview as the header of a uitableview (the cells are user comments, where the header is the "post"):
guard var headerFrame = self.tableView.tableHeaderView?.frame else {return}
headerFrame.size.height = headerFrame.size.height + height
self.header.frame = headerFrame
self.tableView.tableHeaderView = self.header
})
以上代码对我来说非常可靠。我唯一需要做的就是使用 WKPreferences 设置最小字体,因为 WKWebview 不尊重 html 中存在的字体,并且禁用滚动,因为这是调整它的大小以匹配内容.
正在尝试将包含图像的 HTML 内容加载到 UITextView 或 WKWebView 中(我只需要渲染它!)
但是,当我将 HTML 加载到 WKWebView 时,图像不会加载,视图也不会重新加载,并且遵守内容加载之前的自动布局约束:
相关代码在这里:
webView.navigationDelegate = self
webView.loadHTMLString(body, baseURL: nil)
和
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
guard let safeSelf = self else {return}
if let height = result as? CGFloat {
print("height of webview: \(height)")
webView.frame.size.height += height
//Layout subviews?
}
})
}
}
但是,根据
extension String {
var htmlToAttributedString: NSMutableAttributedString? {
guard let data = data(using: .unicode) else { return NSMutableAttributedString() }
do {
return try NSMutableAttributedString(data: data, options: [.documentType: NSMutableAttributedString.DocumentType.html], documentAttributes: nil)
} catch {
return NSMutableAttributedString()
}
}
并将其设置为:
textView.attributedText = htmlString.htmlToAttributedString
这在自动布局方面效果很好...仅适用于文本,但它不保留图像(甚至不显示图像将去向的指示),仅保留文本。不保留 html.
使用的字体或字体大小我直接使用了那个 post 中接受的答案,它不保留图像,即使 OP 要求保留图像。
什么是 a) 将内容加载到 WKWebView 并加载图像的正确方法(我的传输安全设置为允许此演示的任意加载)或 b) 在 UITextView 的解析中保留图像 HTML?我直接得到 html 而不是 URL.
编辑:出于测试目的,我在我的代码中硬编码了一个小样本 html 用于 UIWebView / WKWebView 以反映我从 API 调用中获得的部分内容。
let body = "<div class=\"media-attachment\"><a href=\"https://www.hnmagazine.com/2017/11/07/latinx-really-mean/\"><img src=\"https://testimages.weareher.com/feed/0/2018-08/t9Qc549F7uCtZdbdXx1ERLbXJN2EXIXVacouJDlX.png\"/></a><p class=\"media-caption\">via <a href=\"https://latinxontherise.podbean.com/\">Latinxontherise</a></p></div></div>"
我建议您应该使用 UIWebView 加载 HTML 字符串。记住为 webView 拖动委托,你只调用:your_webview.loadHTMLString(yourHTMLString, baseURL: nil) your_webview.scalesPageToFit = 真
所以,图片没有加载,因为它们是 webP 图片,Apple 不支持 webP(因为它是 google 东西)。
至于自动布局的东西,这是基本的解决方案(对于 WKWebView):
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//A delay here is needed because didFinish does not guarantee content has finished displaying
//Height can only be retrieve after display finishes - hence the delay.
var height: CGFloat = 0.0
dispatch_after(0.1, block: {
height = webView.scrollView.contentSize.height
webView.snp.makeConstraints({make in //I used SnapKit here, because that's how my repo is setup
make.height.equalTo(height)
})
// If the the webview is in another view, update that view's frame with the height + the existing height of that view's frame. For me, I had the webview as the header of a uitableview (the cells are user comments, where the header is the "post"):
guard var headerFrame = self.tableView.tableHeaderView?.frame else {return}
headerFrame.size.height = headerFrame.size.height + height
self.header.frame = headerFrame
self.tableView.tableHeaderView = self.header
})
以上代码对我来说非常可靠。我唯一需要做的就是使用 WKPreferences 设置最小字体,因为 WKWebview 不尊重 html 中存在的字体,并且禁用滚动,因为这是调整它的大小以匹配内容.