Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unnamedd/f0c7347aef964d6c7c1aae5635795309 to your computer and use it in GitHub Desktop.
Save unnamedd/f0c7347aef964d6c7c1aae5635795309 to your computer and use it in GitHub Desktop.
NSAttributedString+Highlight
extension NSAttributedString {
func highlighting(_ substring: String, using color: UIColor) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: self)
attributedString.addAttribute(.foregroundColor, value: color, range: (self.string as NSString).range(of: substring))
return attributedString
}
}
// Usage:
label.attributedText = NSAttributedString(string: "Budapest")
label.attributedText = label.attributedText?.highlighting("Bud", using: .blue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment