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", changeText: "")

Leave a Reply

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

You are commenting using your 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