如果找到搜索栏文本,则更改表图的字母颜色



我有搜索栏和表观。我可以根据搜索栏文本过滤表查看项目。但是我必须更改找到字母的颜色。例如,如果我将他写给搜索栏,我想在表视图中向Hello展示Hello,但他的颜色应该与Llo不同。

感谢您的想法。

使用NSMutableAttributedString作为您的概念,在这里我告诉逻辑自定义您需要的位置(必须在CellForrow中打电话)

    lblcheck.text = "hello" // is the original text 
    lblcheck.textColor = UIColor.red // initally set the whole color for your text
    //Create mutable string from original one
    let attString = NSMutableAttributedString(string: lblcheck.text!)
    //Fing range of the string you want to change colour
    //If you need to change colour in more that one place just repeat it
    let range: NSRange = (lblcheck.text! as NSString).range(of: "he", options: .caseInsensitive) //he in here use searchBar.text
    attString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: range) // here set selection color what ever you typed
    //Add it to the label - notice its not text property but it's attributeText
    lblcheck.attributedText = attString

文本颜色更改可以使用NSAttributedString完成。当您显示搜索结果时,在cellForRow委托方法中,获取搜索字符串并用textView中的CC_4版本替换单词。您可以更改NSAttributedString中特定字符串的颜色。

这是一般的想法,没有看到任何代码。

最新更新