我正在使用 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()
}
答案 0 :(得分:2)
可悲的是,SwiftyMarkdown设置链接范围foregroundColor
的方法似乎并不一致。
As this GitHub issue suggests,您应使用UITextView
而不是UILabel
和设置linkTextAttributes
文本视图。
textView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]
<强>结果:强>
如果您想为链接加下划线,请将underlineStyle
附加到linkTextAttributes
:
textView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red,
NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]
<强>结果:强>