首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将图像从HTML加载到UITextview/WKWebView

将图像从HTML加载到UITextview/WKWebView
EN

Stack Overflow用户
提问于 2018-08-09 03:23:03
回答 2查看 1.2K关注 0票数 1

尝试将包含图像的超文本标记语言内容加载到UITextView或WKWebView中(我只需要渲染它!)

但是,当我将超文本标记语言加载到WKWebView中时,图像不会加载,视图也不会重新加载,也不会遵守内容加载之前的自动布局约束:

相关代码如下:

代码语言:javascript
复制
    webView.navigationDelegate = self
    webView.loadHTMLString(body, baseURL: nil)

代码语言:javascript
复制
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,根据accepted answer found here,我可以执行以下操作:

代码语言:javascript
复制
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()
    }
}

并将其设置为:

代码语言:javascript
复制
textView.attributedText = htmlString.htmlToAttributedString

这在自动布局方面工作得很好……仅用于文本,但它不保存图像(甚至不显示图像将被放置的位置),仅保留文本。而不是事件保留html所使用的字体或字体大小。

我直接使用了那篇文章中被接受的答案,它不保留图像,即使OP要求保留图像。

a)将内容加载到WKWebView和加载图像的正确方法(我的传输安全设置为允许此演示的任意加载)或b)在UITextView的HTML解析中保留图像?我得到的是直接的html,而不是URL。

编辑:出于测试目的,我在我的代码中硬编码了一个小示例html,以便在UIWebView / WKWebView中使用,以反映我从API调用中获得的部分内容。

代码语言:javascript
复制
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>"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-06 00:26:14

所以,图片没有加载是因为它们是webP图片,而苹果不支持webP (因为它是谷歌的东西)。

至于自动布局的东西,这里是基本的解决方案(用于WKWebView):

代码语言:javascript
复制
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中存在的字体,并且禁用了滚动,因为这是调整大小以匹配内容的关键所在。

票数 0
EN

Stack Overflow用户

发布于 2018-08-09 17:13:45

我建议你应该使用UIWebView来加载HTMLString。记住为webView和u仅调用拖动委托: your_webview.loadHTMLString(yourHTMLString,baseURL: nil) your_webview.scalesPageToFit = true

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51754204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档