Skip to content

Instantly share code, notes, and snippets.

@BrayanMC
Created June 27, 2020 19:21
Show Gist options
  • Save BrayanMC/5295c30236e680175354f2ba91b8ae43 to your computer and use it in GitHub Desktop.
Save BrayanMC/5295c30236e680175354f2ba91b8ae43 to your computer and use it in GitHub Desktop.
Change part of the text color.
func attributedStringWithColor(_ strings: [String], color: UIColor,font : UIFont, characterSpacing: UInt? = nil) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: self)
for string in strings {
let range = (self as NSString).range(of: string)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
attributedString.addAttribute(NSAttributedString.Key.font,value: font, range: range)
}
guard let characterSpacing = characterSpacing else {return attributedString}
attributedString.addAttribute(NSAttributedString.Key.kern, value: characterSpacing, range: NSRange(location: 0, length: attributedString.length))
return attributedString
}
/*
let textTitleNotNetwork: NSAttributedString = "¡Uy! No podemos conectarnos".attributedStringWithColor(["conectarnos"], color: UIColor(hexString: "#EF3340"), font: UIFont(fontStyle: .latoRegular, size: 24)!)
lblTitleNotNetwork.attributedText = textTitleNotNetwork
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment