【问题标题】:iOS UItextview link attribute colour not applying to attributed textiOS UItextview链接属性颜色不适用于属性文本
【发布时间】:2022-01-03 12:54:28
【问题描述】:

我有分配给属性文本的 UITextview,它正在正确呈现,但它没有应用链接颜色,在下面 sn-p 链接颜色是 #EB0A1E 但在 textview 它显示为蓝色(默认链接颜色)

textView.attributedText = "Google.com link <a href = \"https://google.com\" style=color:#EB0A1E;>here</a><br><br> Hello there".htmlToAttributedString

extension String {

    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }
}

【问题讨论】:

    标签: ios swift uitextview


    【解决方案1】:

    也许使用@Suragch 的this 答案?它对我有用。我只是用了不同的颜色。

    【讨论】:

    • 我无法在链接属性中将链接颜色硬编码为绿色,它应该采用 html 本身的颜色
    • 我想你不能使用 webview 来显示 HTML?
    • @RamaKrish 我认为仅在 textview 中呈现 HTML 是不可能的。即使这个库link 也不能这样做:“颜色样式不会应用于链接。您必须使用 UITextView 中的 linkTextAttributes 属性来设置它。”
    • @RamaKrish 也许您可以从 HTML 解析颜色,然后将其用作我提出的答案的变量?
    • 我们不能使用 web 视图不知道如何解析 html 和渲染
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2016-04-22
    • 2015-02-24
    相关资源
    最近更新 更多