SimonFairbairn / SwiftyMarkdown

Converts Markdown files and strings into NSAttributedStrings with lots of customisation options.
MIT License
1.64k stars 275 forks source link

[Bug] Link color doesn't work on UILabel #83

Open sger opened 4 years ago

sger commented 4 years ago

For this string:

Lorem ipsum. [apple](https://apple.com/)

changing the color of the link doesn't work

let md = SwiftyMarkdown(string: string)
md.body.color = UIColor.red
md.link.fontSize = 20.0
md.link.color = UIColor.purple
label.attributedText = md.attributedString()
SolorzanoJose commented 4 years ago

Same here.

naremuradyan commented 4 years ago

I also experience this issue. I am using UITextView.

csauvage commented 4 years ago

It's not linked to SwiftyMarkdown but to the behavior of UITextView, simply set your UITextView().tintColor = UIColor.whatEverColorYouWant. It'll be OK.

JeremPlln commented 3 years ago

@sger did you manage to find a solution? I am having the same issue, and modifying the tintColor property of my UILabel doesn't work.

JeremPlln commented 3 years ago

For those who have the issue, I did find a quick fix that involves you to fork the library (a pull request might be better idk).

NSAttributedString.Key.link color will be blue by default. So if you need a custom color you can set the .attachment attribute instead of .link and set the .foregroundColor and .underlineColor with the colors you want. To do so:

  1. Go to the SwiftyMarkdown.swift class
  2. Go to the method attributedStringFor( tokens : [Token], in line : SwiftyLine ) -> NSAttributedString (~ around line 444)
  3. Find the for token in finalTokens loop.
  4. Change every attributes[.link] for attributes[.attachment]

You're good!

NB: If you detect taps on your UILabel in order to open the tapped link inside the UILabel, be sure to also modify .link for .attachment if you use the method enumerateAttribute(_ attrName: NSAttributedString.Key, in enumerationRange: NSRange, options opts: NSAttributedString.EnumerationOptions = [], using block: (Any?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void)