Created
May 25, 2020 12:22
-
-
Save eastari/71d8101baa06a715881f30bd1ba10e8f to your computer and use it in GitHub Desktop.
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 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