I would greatly appreciate if this was updated to Swift 2.0. Please and thanks. (Seriously, I would pay like 10$ to get this updated to Swift 2)
EDIT: OK I have figured out the updated code for the Search and Replace Sections (As well as the highlighting text part) If the article writer or whoever wants to update the article let me know but here is the code:
When you create the RegexHelpers.swift file, this is the code:
extension NSRegularExpression {
convenience init?(options: SearchOptions) {
let searchString = options.searchString
let isCaseSensitive = options.matchCase
let isWholeWords = options.wholeWords
let regexOption: NSRegularExpressionOptions = (isCaseSensitive) ? [] : .CaseInsensitive
let pattern = (isWholeWords) ? “\b(searchString)\b” : searchString
do {
try self.init(pattern: pattern, options: regexOption)
} catch {
print(error)
}
}
}
For the searchForText function:
func searchForText(searchText: String, replaceWith replacementText: String, inTextView textView: UITextView) {
let beforeText = textView.text
let range = NSMakeRange(0, beforeText.characters.count)
if let regex = NSRegularExpression(options: self.searchOptions!) {
let afterText = regex.stringByReplacingMatchesInString(beforeText, options: [], range: range, withTemplate: replacementText)
textView.text = afterText
}
}
For the highlighting function
func highlightText(searchText: String, inTextView textView: UITextView) {
let attributedText = textView.attributedText.mutableCopy() as! NSMutableAttributedString
let attributedTextRange = NSMakeRange(0, attributedText.length)
if let regex = NSRegularExpression(options: self.searchOptions!){
let range = NSMakeRange(0, (textView.text.characters.count))
let matches = regex.matchesInString(textView.text, options: [], range: range)
for match in matches as! [NSTextCheckingResult] {
let matchRange = match.range
attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: matchRange)
}
}
textView.attributedText = attributedText.copy() as! NSAttributedString
}