Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eastari/71d8101baa06a715881f30bd1ba10e8f to your computer and use it in GitHub Desktop.
Save eastari/71d8101baa06a715881f30bd1ba10e8f to your computer and use it in GitHub Desktop.
extension NSMutableAttributedString {
static func generateAttributedString(with searchTerm: String, targetString: String) -> NSMutableAttributedString? {
let attributedString = NSMutableAttributedString(string: targetString)
do {
// Your more complicated expressions here
let regex = try NSRegularExpression(pattern: searchTerm, options: .caseInsensitive)
let range = NSRange(location: 0, length: targetString.utf16.count)
for match in regex.matches(in: targetString, options: .withTransparentBounds, range: range) {
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold), range: match.range)
}
return attributedString
} catch {
print("Error creating regular expresion: \(error)")
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment