Skip to content

Instantly share code, notes, and snippets.

@eddieespinal
Created November 12, 2015 18:56
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 eddieespinal/e71aa0ffaa82fc414687 to your computer and use it in GitHub Desktop.
Save eddieespinal/e71aa0ffaa82fc414687 to your computer and use it in GitHub Desktop.
let searchString = "this"
let baseString = "This is some string that contains the word \"this\" more than once. This substring has multiple cases. ThisthisThIs."
let attributed = NSMutableAttributedString(string: baseString)
var error: NSError?
let regex = NSRegularExpression(pattern: searchString, options: .CaseInsensitive, error: &error)
if let regexError = error {
println("Oh no! \(regexError)")
} else {
for match in regex?.matchesInString(baseString, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: baseString.utf16Count)) as [NSTextCheckingResult] {
attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range)
}
textView.attributedText = attributed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment