【问题标题】:Label shows the html data and make image and link clickable by swift标签显示 html 数据并通过 swift 使图像和链接可点击
【发布时间】:2017-11-24 08:49:36
【问题描述】:

我的标签中有一个 html 数据,以及如何使它们可点击。
我可以成功显示我的 html 数据,但我不知道如何转换它们。
有什么好的建议吗?
谢谢

htmlData content here.

这是我的标签代码:

let htmlData = ""

do {
        let attrStr = try NSAttributedString(
            data: htmlData.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
        self.label.attributedText = attrStr
    } catch let error {
        print(error)
    }

【问题讨论】:

  • 使用UITextView,而不是UILabelUILabel 不是可点击的。你可以做到,但这有点棘手,在 CocoaPods/GitHub/CocoaControls 等中已经有很多解决方案。更简单的方法是使用 UITextView 并稍加修改,它看起来就像一个 UILabel用户。
  • 对不起,有样品给我吗?
  • 对不起,我不知道如何重复。只是字符串链接我知道该怎么做。但是像我的图片这样的链接在我的 html 数据字符串中。我不知道该怎么做。

标签: ios swift uilabel


【解决方案1】:

标签可能无法检测超链接,但“webView”可以。

webView.loadHTMLString(dataHtmlString, baseURL: nil)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2014-08-06
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多