如何使用SwiftyMarkdown更改链接颜色?

时间:2018-01-20 01:10:33

标签: ios swift4

我正在使用 SwiftyMarkdown 。我写了下面的代码,但我无法更改链接颜色。 我怎样才能改变它的颜色?

private func markdown(_ markdownText: String) -> SwiftyMarkdown {
    let md = SwiftyMarkdown(string: markdownText)
    md.setFontColorForAllStyles(with: .white)
    md.link.color = .red
    return md
}

func sample() {
    label?.attributedText = markdown("sample [url](http://google.com)").attributedString()
}

1 个答案:

答案 0 :(得分:2)

可悲的是,SwiftyMarkdown设置链接范围foregroundColor的方法似乎并不一致。

As this GitHub issue suggests您应使用UITextView 而不是UILabel设置linkTextAttributes 文本视图。

textView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]

<强>结果:

screenshot of the result

如果您想为链接加下划线,请将underlineStyle附加到linkTextAttributes

textView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red,
                               NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]

<强>结果:

screenshot of the result (the link is underlined)