Colorie toutes les occurrences de string dans swift

Ce code

var textSearch="hi" var textToShow="hi hihi hi" var rangeToColor = (textToShow as NSSsortingng).rangeOfSsortingng(textSearch) var atsortingbutedSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng:textToShow) atsortingbutedSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: UIColor.yellowColor() , range: rangeToColor) TextView.atsortingbutedText=atsortingbutedSsortingng 

me donne NSRange pour colorer une string à l'intérieur du TextView. Le problème est que je ne renvoie que la première occurrence. Si le mot contient "hi hihi hi", seul le premier "hi" est coloré. Comment puis-je get toutes les occurrences de la string?

Swift 3

 let attrStr = NSMutableAtsortingbutedSsortingng(ssortingng: "hi hihi hey") let inputLength = attrStr.ssortingng.characters.count let searchSsortingng = "hi" let searchLength = searchSsortingng.characters.count var range = NSRange(location: 0, length: attrStr.length) while (range.location != NSNotFound) { range = (attrStr.ssortingng as NSSsortingng).range(of: searchSsortingng, options: [], range: range) if (range.location != NSNotFound) { attrStr.addAtsortingbute(NSForegroundColorAtsortingbuteName, 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 = NSMutableAtsortingbutedSsortingng(ssortingng: "hi hihi hey") let inputLength = attrStr.ssortingng.characters.count let searchSsortingng = "hi" let searchLength = searchSsortingng.characters.count var range = NSRange(location: 0, length: attrStr.length) while (range.location != NSNotFound) { range = (attrStr.ssortingng as NSSsortingng).rangeOfSsortingng(searchSsortingng, options: [], range: range) if (range.location != NSNotFound) { attrStr.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: UIColor.yellowColor(), range: NSRange(location: range.location, length: searchLength)) range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length)) } } 

Sucre de syntaxe pour la réponse de Kevin ci-dessus.

Appelé comme:

 attrStr.atsortingbuteRangeFor(searchSsortingng, atsortingbuteValue: UIColor.yellowColor(), asortingbuteSearchType: .All) 

Swift 2.0:

 import UIKit extension NSMutableAtsortingbutedSsortingng { enum AsortingbuteSearchType { case First, All, Last } func atsortingbuteRangeFor(searchSsortingng: Ssortingng, atsortingbuteValue: AnyObject, asortingbuteSearchType: AsortingbuteSearchType) { let inputLength = self.ssortingng.characters.count let searchLength = searchSsortingng.characters.count var range = NSRange(location: 0, length: self.length) var rangeCollection = [NSRange]() while (range.location != NSNotFound) { range = (self.ssortingng as NSSsortingng).rangeOfSsortingng(searchSsortingng, options: [], range: range) if (range.location != NSNotFound) { switch asortingbuteSearchType { case .First: self.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: atsortingbuteValue, range: NSRange(location: range.location, length: searchLength)) return case .All: self.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: atsortingbuteValue, 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 asortingbuteSearchType { case .Last: let indexOfLast = rangeCollection.count - 1 self.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: atsortingbuteValue, range: rangeCollection[indexOfLast]) break default: break } } } 

L'utilisation de NSRegularExpression vous évite d'effectuer tous les calculs de plage par vous-même. Cet exemple mettra également en évidence deux mots au lieu d'un seul.

 let text = "If you don't have a plan, you become part of somebody else's plan." let toHighlight = ["plan", "you"] let range = text.nsRange(from: text.startIndex ..< text.endIndex) // full text let rangesToHighlight: [[NSRange]] = toHighlight.map { search in do { let regex = try NSRegularExpression(pattern: search, options: []) let matches: [NSTextCheckingResult] = regex.matches(in: text, options: [], range: range) return matches.map { $0.range } // get range from NSTextCheckingResult } catch { return [NSRange]() } } let yellow = UIColor.yellow let atsortingbutedText = NSMutableAtsortingbutedSsortingng(ssortingng: text) let flattenedRanges: [NSRange] = rangesToHighlight.joined() flattenedRanges.forEach { // apply color to all ranges atsortingbutedText.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: yellow, range: $0) }