2

I'm having some search option in my app, which will highlight given word in UISearchBar. Given word may occurs multiple time in label I need t highlight all those words. How it is possible, I have tried with some set of codes but It will highlight only one occurrence of that word, Here is my sample code:

var SearchAttributeText = "The"
let range = (TextValue as NSString).range(of: SearchAttributeText)
let attribute = NSMutableAttributedString.init(string: TextValue)
attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
self.label.attributedText = attribute

Need to support with both Upper and lower cases. Word The may occurs multiple time need to highlight all.

Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47
  • See this tutorial : https://iosdevcenters.blogspot.com/2015/12/how-to-set-use-multiple-font-colors-in.html – Bhadresh Dec 11 '17 at 08:51
  • @Bhadresh I already referred this blog they change color based on range not based to given character – Kavin Kumar Arumugam Dec 11 '17 at 08:56
  • Get all ranges of the particular substring with the help of [this question](https://stackoverflow.com/questions/36865443/get-all-ranges-of-a-substring-in-a-string-in-swift) and iterate through them. – Tamás Sengel Dec 11 '17 at 09:15
  • You can use a `NSRegularExpression` or a for loop on `range(of:)` (note that `range(of:)` has "variants" with options like case insensitive) to get all ranges of the occurrences of your searched text. – Larme Dec 11 '17 at 09:40

1 Answers1

1

You can use following code to search in string

    //Text need to be searched
    let SearchAttributeText = "the"

    //Store label text in variable as NSString
    let contentString = lblContent.text! as NSString

    //Create range of label text
    var rangeString = NSMakeRange(0, contentString.length)

    //Convert label text into attributed string
    let attribute = NSMutableAttributedString.init(string: contentString as String)

    while (rangeString.length != NSNotFound && rangeString.location != NSNotFound) {

        //Get the range of search text
        let colorRange = (lblContent.text?.lowercased() as! NSString).range(of: SearchAttributeText, options: NSString.CompareOptions(rawValue: 0), range: rangeString)

        if (colorRange.location == NSNotFound) {
            //If location is not present in the string the loop will break
            break
        } else {
            //This line of code colour the searched text
            attribute.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: colorRange)
            lblContent.attributedText = attribute

            //This line of code increment the rangeString variable
            rangeString = NSMakeRange(colorRange.location + colorRange.length, contentString.length - (colorRange.location + colorRange.length))
        }
    }

The below line of code update the range by incrementing the location and length parameter of NSRange

rangeString = NSMakeRange(colorRange.location + colorRange.length, contentString.length - (colorRange.location + colorRange.length))
Indrajeet
  • 5,490
  • 2
  • 28
  • 43