Search code examples
swiftstringtextcolorsnsattributedstring

Color a string based on occurrences in a Variable String


Im trying to create a function which will allow an input string to have certain words coloured and then the function wouldn return a coloured string.

I started with the red colour first but can’t figure out how to make it work.

My code so far:

let oldString = "TEST STRING TO COLOUR IT WORDS EXIST" //sample of a variable string that may or may not contain wors that need coloring

let newString = stringColorCoding(stringToColor: oldString, colorRed: "TO, POT, TEST", colorYellow: "EXIST, TOP", colorGreen: "AB, +TA, -XY, WORDS")

func stringColorCoding(stringToColor: String, colorRed: String, colorYellow: String, colorGreen: String)
{

let attrStr = NSMutableAttributedString(string: stringToColor)
let inputLength = attrStr.string.count
let searchStringRed = colorRed
let searchLengthRed = searchStringRed.characters.count
var rangeRed = NSRange(location: 0, length: attrStr.length)

while (range.location != NSNotFound) 
{
  range = (attrStr.string as NSString).range(of: searchStringRed, options: [], range: range)
     if (range.location != NSNotFound) 
   {
     attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.systemRed, range: NSRange(location: range.location, length: searchLengthRed))
     range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
     }
  }
 return attrStr
}

Solution

  • The way you are going about it is quite complicated. I'd use enumerateSubstrings to get each word in the string. And then instead of passing in a comma-separated string with search terms I'd pass in an array of search strings.

    extension String {
        func highlighted(
            redText: [String],
            yellowText: [String],
            greenText: [String]
        ) -> NSAttributedString {
    
            let result = NSMutableAttributedString(string: self)
            enumerateSubstrings(in: startIndex..<endIndex, options: .byWords) {
                (substring, substringRange, _, _) in
                guard let substring = substring else { return }
                if redText.contains(substring) {
                    result.addAttribute(
                        .foregroundColor,
                        value: UIColor.systemRed,
                        range: NSRange(substringRange, in: self)
                    )
                }
                if yellowText.contains(substring) {
                    result.addAttribute(
                        .foregroundColor,
                        value: UIColor.systemYellow,
                        range: NSRange(substringRange, in: self)
                    )
                }
                if greenText.contains(substring) {
                    result.addAttribute(
                        .foregroundColor,
                        value: UIColor.systemGreen,
                        range: NSRange(substringRange, in: self)
                    )
                }
            }
            return result
        }
    }
    

    The usage is as follows:

    let highlighted = "TEST TO COLOUR IT WORDS EXIST".highlighted(
        redText: ["TO", "POT", "TEST"],
        yellowText: ["EXIST", "TOP"],
        greenText: ["AB", "+TA", "-XY", "WORDS"]
    )