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.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- es 单字段多分词器时,textField.keyword无法高亮
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- Popover segue to static cell UITableView causes co
- How do you detect key up / key down events from a
@Vijay has a good, correct answer for this post.
I modified this slightly for my own purposes (bolding the text in my search results) by creating a function that accepts the
searchString
and the string you want to modify - theresultString
- and returns anattributedString
that can be applied to aUILabel
.I also had this check for the
lowercaseString
attribute, so that regardless of what I typed into my search bar, my string would match the characters rather than the case (this requirement may be different, depending on your use-case).Note: I had a custom bolded font to work with, but you could always use
UIFont.boldSystemFontOfSize(fontSize: CGFloat)
or something like that to get a similar effect.Then, just add the result to your label by doing (some variation of):
You could achieve this by find the search term string range from your result string and adding attributed string at that range. Find the sample code below,
Objective-C
Swift (TESTED)
Tried to keep it as slim as possible. The function returns an attributed string with searchText occurrences in bold. Swift 4.2+
In the cell you will need to add code like this for configuration: