0

I have implemented a UISearchBar in a UITableView. Now I want to highlight the result of search list. For example, if I am typing three letters then those letters should be highlighted in the resultant TableView that drops down from the search bar.

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
Deviyani Swami
  • 749
  • 8
  • 17
  • 1
    See this: https://stackoverflow.com/questions/33542905/highlighting-search-result-in-uitableview-cell-ios-swift – Mahak Mittal Feb 06 '19 at 11:51
  • 2
    Please provide some code. What is your approach? What is going right? What is going wrong? Expected output? Current output? – Gustavo Vollbrecht Feb 06 '19 at 11:52
  • 2
    Hey, and welcome to stackoverflow. I'm afraid no appropriate answer can be given with this little information about what you need. In stackoverflow, try to ask questions with what you have done, post your code and screenshots of the issue, that way we all can understand better – excitedmicrobe Feb 06 '19 at 11:52

1 Answers1

0

func boldSearchResult(searchString: String, resultString: String) -> NSMutableAttributedString {

let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: resultString)
let Desiredpattern = searchString.lowercaseString
let range: NSRange = NSMakeRange(0, resultString.characters.count)

let regex = try! NSRegularExpression(Desiredpattern: Desiredpattern, options: NSRegularExpressionOptions())

regex.enumerateMatchesInString(resultString.lowercaseString, options: NSMatchingOptions(), range: range) { (textCheckingResult, matchingFlags, stop) -> Void in
    let Range = textCheckingResult?.range
    attributedString.addAttribute(NSFontAttributeName, value: UIFont.customBoldedFontNameWithSize(15.0), range: Range!)
}

return attributedString

}

cell.YourLabel.attributedText = boldSearchResult(mySearchText, resultString: textForBolding)
Nishant Pathania
  • 349
  • 1
  • 14