Nishant Chandwani
Nishant Chandwani

Reputation: 445

How to change the color of NSAttributedStringKey.link Swift4

I am trying to change the color of uitextview link attribute. But i am cant able to get success. Please Help me here is my code.

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.

  txt_text.delegate = self
            let main_string:String = "test this text in #nishant #waooo good to #UUUUUU"
            // print("---->",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<String.Index>) -> 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) -> Range<String.Index>? {
        return Range(nsRange, in: self)
    }

}

Please give me the solution

Upvotes: 1

Views: 1078

Answers (1)

Joshua Wolff
Joshua Wolff

Reputation: 3352

Change the textview tint color.

let textvw = UITextView()
textvw.tintColor = .blue

Upvotes: 1

Related Questions