ios - 如果找到搜索栏文本,则更改表格 View 的字母颜色

标签 ios swift uitableview swift3 uisearchbar

我有搜索栏和表格 View 。我可以根据搜索栏文本过滤表格 View 项目。但我必须改变发现字母的颜色。例如,如果我将 HE 写到搜索栏,我想在表格 View 中显示 HELLO,但 HE 的颜色应该与 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

关于ios - 如果找到搜索栏文本,则更改表格 View 的字母颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46301230/

相关文章:

ios - 在 frida-trace onEnter 中停止函数调用

ios - swift 3.0 : Timer not firing for target other than self

ios - 核心图 - 如何在图下显示轴标签?

ios - 如何在 UIView 中独立绘制到 CAShapeLayer?

ios - 手机间隙 : Calling an action when an app is opened after clicking the home button (iOS)

ios - didSelectRowAtIndexPath返回界限索引

ios - tableView 单元格中标签的框架大小不会改变

ios - 快速警报自定义显示和分配

ios - 断断续续的全屏退出过渡

ios - 在 UITableView 中删除行但不使用删除按钮