Created
November 12, 2015 18:56
-
-
Save eddieespinal/e71aa0ffaa82fc414687 to your computer and use it in GitHub Desktop.
Revisions
-
eddieespinal created this gist
Nov 12, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ 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 }