Created
November 12, 2015 18:56
-
-
Save eddieespinal/e71aa0ffaa82fc414687 to your computer and use it in GitHub Desktop.
UITextview highlight all matches swift.. http://stackoverflow.com/questions/25525171/uitextview-highlight-all-matches-swift
This file contains 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
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