在UITableView单元格iOS Swift中突出显示搜索结果 [英] Highlighting search result in UITableView cell iOS Swift
本文介绍了在UITableView单元格iOS Swift中突出显示搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在TableView
中实现了搜索栏.现在,我要突出显示结果.例如,如果我键入了两个字母,则这两个字母应在从搜索栏下拉的结果TableView
中突出显示.谁能帮我做到这一点?我知道我应该为此使用自定义单元格,但是却迷失了它.
I have implemented a search bar in a TableView
. Now I want to highlight the result. For example, if I have typed two letters then those two letters should be highlighted in the resultant TableView
that drops down from the search bar. Can anyone help me to do this? I know I should use custom cells for this, but am lost implementing it.
推荐答案
这是在表格视图中为文本添加属性的第二种方法
Here is the second way to attribute text in tableview
let initialtext = "Hello World"
let attrString: NSMutableAttributedString = NSMutableAttributedString(string: initialtext)
let range: NSRange = (initialtext as NSString).rangeOfString(("World" , options:NSStringCompareOptions.CaseInsensitiveSearch])
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
cell!.textLabel?.attributedText = attrString
这篇关于在UITableView单元格iOS Swift中突出显示搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文