Forked from vinczebalazs/NSAttributedStringHighlight.swift
Created
June 29, 2020 07:19
-
-
Save unnamedd/f0c7347aef964d6c7c1aae5635795309 to your computer and use it in GitHub Desktop.
NSAttributedString+Highlight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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