【问题标题】:How to display content text with html tags and images in swift 3 ios如何在swift 3 ios中显示带有html标签和图像的内容文本
【发布时间】:2017-06-22 23:41:15
【问题描述】:

我收到来自API 的文本,其中包含html tagspictures。如何显示它,以及我需要显示的位置,我的意思是UILabelWebView? 我尝试使用NSAttributedString,但不是我需要的。

平台 iOS 9、Swift 3

【问题讨论】:

    标签: html ios swift webview


    【解决方案1】:

    我找到方法了。 我在Info.plist

    应用传输安全设置 -> 允许任意加载 -> 是

    并且图像正在显示。

    和代码:

    DispatchQueue.main.async {
        let attrStr = try! NSAttributedString(
                    data: (self.detailLabelText?.data(using: String.Encoding.unicode, allowLossyConversion: true)!)!,
                    options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                    documentAttributes: nil)
    
       self.detailLabel.attributedText = attrStr
       self.detailLabel.textAlignment = NSTextAlignment.justified
       self.detailLabel.contentMode = .scaleToFill
       self.detailLabel.font = UIFont(name: "PTSans-Narrow", size: 18.0)
    }
    
    detailLabel.adjustsFontSizeToFitWidth = true
    detailLabel.sizeToFit()
    

    【讨论】:

    • Info.plist 正是我所缺少的!
    • 图片内容呢?它将从 uilabel 中删除?还是会显示?
    • @Zhanserik 最佳答案:)
    • 你能帮我解决这个问题吗:stackoverflow.com/questions/57702924/…
    • @jazzbpn 抱歉,我没有使用 Flutter 的经验。推荐编写原生应用。
    【解决方案2】:

    你可以在webView中加载html标签

     func loadHtmlCode() {
            let htmlCode = "<html><head><title>Wonderful web</title></head> <body><p>wonderful web. loading html code in <strong>UIWebView</strong></></body>"
            webView.loadHTMLString(htmlCode, baseURL: nil)
        }
    

    【讨论】:

    • 问题 Web 视图(已弃用)。任何对苹果有良好经验的人。因为 webkit 有一个不应该被允许的导航。因为它们会在页面中丢失
    猜你喜欢
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多