title: How to change the color of NSAttributedStringKey.link Swift4
body: <p>I am trying to change the color of <code>uitextview</code> link attribute. But i am cant able to get success. Please Help me here is my code. </p>
  
  <p>I have one string name is the main string .IN this string i have hashtag string i am founding the hashtag value and converting in the clickable link . The problem is I want to change this hashtag text color as red. </p>
  
  <pre><code>  txt_text.delegate = self
              let main_string:String = "test this text in #nishant #waooo good to #UUUUUU"
              // print("----&gt;",main_string)
              let attributedString = NSMutableAttributedString(string:main_string)
  
              let font = UIFont(name: "verdana", size: 13.0)
  
              attributedString.addAttribute(NSAttributedStringKey.font, value:font!, range: NSRange.init(location: 0, length: main_string.utf16.count))
              let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: [])
              // print(regex)
              let matches = regex?.matches(in: main_string, options: [], range: NSRange(location: 0, length: main_string.utf16.count))
  
  
  
              for match: NSTextCheckingResult in matches! {
                  let wordRange: NSRange = match.range(at: 0)
  
      //                            let linkAttributes = [
      //                                NSAttributedStringKey.link: URL(string: "https://www.apple.com")!,
      //                                NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 18.0)!,
      //                                NSAttributedStringKey.foregroundColor.rawValue: UIColor.red
      //                                ] as! [String : Any]
  
                  let tet  = main_string.rangeFromNSRange(nsRange: wordRange)
                  let HasTagText = main_string[tet!]
                 // print("-----",HasTagText)
  
                //  attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: wordRange)
                  attributedString.addAttribute(NSAttributedStringKey.link, value: URL(string: String(HasTagText))! , range: wordRange)
                  attributedString.addAttribute(NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue), value: UIColor.red , range: wordRange)
  
                 // attributedString.addAttributes(linkAttributes, range: wordRange)
  
              }
  
  
              self.txt_text.attributedText = attributedString
  
  
  
  
  
  extension String {
  
      func nsRange(from range: Range&lt;String.Index&gt;) -&gt; NSRange? {
          let utf16view = self.utf16
          if let from = range.lowerBound.samePosition(in: utf16view), let to = range.upperBound.samePosition(in: utf16view) {
              return NSMakeRange(utf16view.distance(from: utf16view.startIndex, to: from), utf16view.distance(from: from, to: to))
          }
          return nil
      }
  
  
      func rangeFromNSRange(nsRange : NSRange) -&gt; Range&lt;String.Index&gt;? {
          return Range(nsRange, in: self)
      }
  
  }
  </code></pre>
  
  <p>Please give me the solution </p>