我在UISearchBar
中实现了UITableView
。现在,我要突出显示搜索列表的结果。例如,如果我输入三个字母,则这些字母应在从搜索栏下拉的结果TableView中突出显示。
答案 0 :(得分: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)