How to create attributed text with diff colors in Swift


    func halfTextColorChange (fullText : String , changeText : String ) -> NSMutableAttributedString{
        let strNumber: NSString = fullText as NSString
        let range = (strNumber).range(of: changeText)
        
        let attribute = NSMutableAttributedString.init(string: fullText)
        attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(red: 32/255, green: 20/255, blue: 235/255, alpha: 1.0).cgColor , range: range)
        return attribute
    }

//Call function as required
   yourlabel.text = halfTextColorChange(fullText: "To reset your password send an email to sardar.sandesh@gmail.com", changeText: "sardar.sandesh@gmail.com")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s