Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link color does not work? #50

Open
amiantos opened this issue Sep 11, 2019 · 4 comments
Open

Link color does not work? #50

amiantos opened this issue Sep 11, 2019 · 4 comments

Comments

@amiantos
Copy link

Hi! I was glad to find this project, I was trying to use Down, but it's attributed string rendering leaves much to be desired. I thought this seemed like a great way to get a dark mode friendly Markdown parser into my project... and it is! So, thank you.

Unfortunately there appears to be a problem with link colors. Here's some example code...

var textColor: UIColor = .black
if #available(iOS 13.0, *) {
    textColor = .label
}
let markdownParser = MarkdownParser(font: UIFont.preferredFont(forTextStyle: .body), color: textColor)
markdownParser.link.color = textColor
return markdownParser.parse("A string [with a link](http://foo.com), and so on.")

While the main content of the string is colored properly no matter what, the link color appears to be stuck as a black color, even without the link.color call. I've tried link.color = .red as well, and no dice.

@amiantos
Copy link
Author

After some surfing I found a solution, though it's outside of MarkdownKit:

var linkColor: UIColor = .darkGray
var underlineColor: UIColor = .lightGray
if #available(iOS 13.0, *) {
    linkColor = .secondaryLabel
    underlineColor = .quaternaryLabel
}
let linkAttributes: [NSAttributedString.Key: Any] = [
    NSAttributedString.Key.foregroundColor: linkColor,
    NSAttributedString.Key.underlineColor: underlineColor,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
textView.linkTextAttributes = linkAttributes

@amiantos amiantos changed the title Link color does not work? (Maybe: Dark Mode related) Link color does not work? Sep 11, 2019
@phlippieb
Copy link

I think the cause is as follows.

For links, the color property is used to compute attributes - see https://github.com/bmoliveira/MarkdownKit/blob/master/MarkdownKit/Sources/Common/Protocols/MarkdownStyle.swift

However, when text is actually formatted, those attributes are not used. Instead, only the .link key of the attributed value is set. See https://github.com/bmoliveira/MarkdownKit/blob/master/MarkdownKit/Sources/Common/Elements/Link/MarkdownLink.swift#L41:

attributedString.addAttribute(NSAttributedString.Key.link, value: url, range: range)

So setting color has no effect.

@iJACD
Copy link

iJACD commented Jun 23, 2020

Sorry to prod but any working solution to this issue yet? @bmoliveira

@gaebel
Copy link
Contributor

gaebel commented Apr 25, 2021

The link color attribute gets overriden by the textViews' linkTextAttributes. To use the color that you defined via the parser you must clear the linkTextAttributes like this self.textView.linkTextAttributes = [:]. You can also just alter the tintColor of your textView directly or like @amiantos showed above for a more fine grained control self.textView.linkTextAttributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants