Color all occurrences of string in swift Color all occurrences of string in swift swift swift

Color all occurrences of string in swift


Swift 5

let attrStr = NSMutableAttributedString(string: "hi hihi hey")let inputLength = attrStr.string.countlet searchString = "hi"let searchLength = searchString.characters.countvar range = NSRange(location: 0, length: attrStr.length)while (range.location != NSNotFound) {    range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)    if (range.location != NSNotFound) {        attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.yellow, range: NSRange(location: range.location, length: searchLength))        range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))    }}

Swift 3

let attrStr = NSMutableAttributedString(string: "hi hihi hey")let inputLength = attrStr.string.characters.countlet searchString = "hi"let searchLength = searchString.characters.countvar range = NSRange(location: 0, length: attrStr.length)while (range.location != NSNotFound) {    range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)    if (range.location != NSNotFound) {        attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow(), range: NSRange(location: range.location, length: searchLength))        range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))    }}

Swift 2

let attrStr = NSMutableAttributedString(string: "hi hihi hey")let inputLength = attrStr.string.characters.countlet searchString = "hi"let searchLength = searchString.characters.countvar range = NSRange(location: 0, length: attrStr.length)while (range.location != NSNotFound) {    range = (attrStr.string as NSString).rangeOfString(searchString, options: [], range: range)    if (range.location != NSNotFound) {        attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor(), range: NSRange(location: range.location, length: searchLength))        range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))    }}


Swift 4:

let string = "foo fbar foofoo foofo"let mutableAttributedString = NSMutableAttributedString(string: string)let searchString = "foo"var rangeToSearch = string.startIndex..<string.endIndexwhile let matchingRange = string.range(of: searchString, options: [], range: rangeToSearch) {  mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.yellow, range: NSRange(matchingRange, in: string))  rangeToSearch = matchingRange.upperBound..<string.endIndex}


Syntax sugar for Kevin's answer above.

Called like:

attrStr.attributeRangeFor(searchString, attributeValue: UIColor.yellowColor(), atributeSearchType: .All)

Swift 2.0:

import UIKitextension NSMutableAttributedString {    enum AtributeSearchType {        case First, All, Last    }    func attributeRangeFor(searchString: String, attributeValue: AnyObject, atributeSearchType: AtributeSearchType) {        let inputLength = self.string.characters.count        let searchLength = searchString.characters.count        var range = NSRange(location: 0, length: self.length)        var rangeCollection = [NSRange]()        while (range.location != NSNotFound) {            range = (self.string as NSString).rangeOfString(searchString, options: [], range: range)            if (range.location != NSNotFound) {                switch atributeSearchType {                case .First:                    self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: NSRange(location: range.location, length: searchLength))                    return                case .All:                    self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: NSRange(location: range.location, length: searchLength))                    break                case .Last:                    rangeCollection.append(range)                    break                }                range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))            }        }        switch atributeSearchType {        case .Last:            let indexOfLast = rangeCollection.count - 1            self.addAttribute(NSForegroundColorAttributeName, value: attributeValue, range: rangeCollection[indexOfLast])            break        default:            break        }    }}