0

I am trying to change the text colour of all instances of "(R U R' U')" in a UILabel to red, and other instances of (ALG_HERE) to show as different colours such as blue and orange.

So far I have this

     @IBOutlet weak var algName: UILabel?




              algToShow = UserDefaults.standard.string(forKey: "ollShowAlg\(OLLData.OLLCasesList[selectedCases[scrambleNumber]-1]._id)")!

    //let text = "This is the text and i want to replace something"
    let mutableAttributedString = NSMutableAttributedString(string: algToShow)

    var searchRange = NSRange(location: 0, length: algToShow.count)
    var foundRange1 = NSRange()
    var foundRange2 = NSRange()
    var foundRange3 = NSRange()
    var foundRange4 = NSRange()
    var foundRange5 = NSRange()

    while searchRange.location < algToShow.count {
        searchRange.length = algToShow.count - searchRange.location
        foundRange1 = (algToShow as NSString).range(of: "(R U R' U')", options: NSString.CompareOptions.caseInsensitive, range: searchRange) //sexy
        foundRange2 = (algToShow as NSString).range(of: "(R' F R F')", options: NSString.CompareOptions.caseInsensitive, range: searchRange) //sledge
        foundRange3 = (algToShow as NSString).range(of: "(R U R' U)", options: NSString.CompareOptions.caseInsensitive, range: searchRange)
        foundRange4 = (algToShow as NSString).range(of: "(L' U' L U)", options: NSString.CompareOptions.caseInsensitive, range: searchRange) //anti-sexy
        foundRange5 = (algToShow as NSString).range(of: "(F R U R' U' F')", options: NSString.CompareOptions.caseInsensitive, range: searchRange) //F-sexy-F'

        if foundRange1.location != NSNotFound || foundRange2.location != NSNotFound || foundRange3.location != NSNotFound || foundRange4.location != NSNotFound || foundRange5.location != NSNotFound {
            // found an occurrence of the substring! do stuff here
            searchRange.location = foundRange1.location + foundRange1.length
            mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: foundRange1)
            searchRange.location = foundRange2.location + foundRange2.length
            mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: foundRange2)
            searchRange.location = foundRange3.location + foundRange3.length
            mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.orange, range: foundRange3)
            searchRange.location = foundRange4.location + foundRange4.length
            mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.brown, range: foundRange4)
            searchRange.location = foundRange5.location + foundRange5.length
            mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: foundRange5)

        } else {
            // no more substring to find
            break
        }
    }

    //Apply
    algName!.attributedText = mutableAttributedString;

This is what is currently displayed. enter image description here

As you can see in the image the second instance of (R' F R F') isnt BLUE like it should be. If i adapt my code slightly I can make it work but then the front (R U R' U) looses its ORANGE colour.

I cant seem to make both of them work in conjuction with one-another.

LukeTerzich
  • 555
  • 1
  • 4
  • 17
  • 1
    There is a Swift answer in the duplicate. – rmaddy May 08 '19 at 20:26
  • @rmaddy this works perfectly, I have editted my question now. The duplicate will make all instances RED (so that works) but I cannot make then all instances of another range a different colour (such as BLUE or GREEN). – LukeTerzich May 09 '19 at 06:54

0 Answers0