我在TableView
中實現了一個搜索欄。現在我想強調結果。例如,如果我輸入了兩個字母,那麼這兩個字母應該在從搜索欄下拉的結果TableView
中突出顯示。任何人都可以幫助我做到這一點?我知道我應該使用自定義單元格,但我失去了實現它。在UITableView單元格中突出顯示搜索結果iOS Swift
2
A
回答
3
您可以通過從結果字符串中找到搜索字詞字符串範圍並在該範圍內添加屬性字符串來實現此目的。請看下面的示例代碼,
Objective-C的
NSString *searchTerm = /* SEARCH_TERM */;
NSString *resultText = /* YOUR_RESULT_TEXT */;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:resultText];
NSString *pattern = [NSString stringWithFormat:@"(%@)", searchTerm];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:kNilOptions error:nil];
NSRange range = NSMakeRange(0, resultText.length);
[regex enumerateMatchesInString:resultText options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange subStringRange = [result rangeAtIndex:1];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:subStringRange];
}];
斯威夫特(測試)
let searchTerm = "" /* SEARCH_TERM */
let resultText = "" /* YOUR_RESULT_TEXT */
let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: resultText)
let pattern = "(\(searchTerm))"
let range:NSRange = NSMakeRange(0, resultText.characters.count)
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions())
regex.enumerateMatchesInString(
resultText,
options: NSMatchingOptions(),
range: range,
usingBlock: {
(textCheckingResult, matchingFlags, stop) -> Void in
let subRange = textCheckingResult?.range
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: subRange!)
}
)
6
@Vijay對這個崗位創先爭優,正確的答案。
我修改這個稍微對我自己的目的(粗體在我的搜索結果中的文本)通過創建接受searchString
函數和字符串要修改 - 在 resultString
- 並返回一個attributedString
可應用於一個UILabel
。
我也對lowercaseString
屬性進行了檢查,因此無論我在搜索欄中輸入什麼內容,我的字符串都會匹配字符而不是大小寫(根據您的使用情況,此要求可能會有所不同) 。
func boldSearchResult(searchString: String, resultString: String) -> NSMutableAttributedString {
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: resultString)
let pattern = searchString.lowercaseString
let range: NSRange = NSMakeRange(0, resultString.characters.count)
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions())
regex.enumerateMatchesInString(resultString.lowercaseString, options: NSMatchingOptions(), range: range) { (textCheckingResult, matchingFlags, stop) -> Void in
let subRange = textCheckingResult?.range
attributedString.addAttribute(NSFontAttributeName, value: UIFont.customBoldedFontNameWithSize(15.0), range: subRange!)
}
return attributedString
}
注意:我有一個自定義的字體加粗的工作,但你總是可以使用UIFont.boldSystemFontOfSize(fontSize: CGFloat)
或類似的東西來獲得類似的效果。
然後,只需做(的某種變體)的結果添加到您的標籤:
cell.myCustomLabel.attributedText = boldSearchResult(mySearchText, resultString: textForBolding)
8
這裏是一個屬性文字在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
相關問題
- 1. 使Swift UITableView的單元格在突出顯示時突出顯示
- 2. ASP.NET MVC - 在搜索結果中突出顯示搜索字詞
- 3. 突出顯示搜索結果
- 4. 突出顯示部分搜索結果
- 5. 如何突出顯示PDFKit中的搜索結果 - iOS
- 6. UITableView - 突出顯示所選單元格[iOS]
- 7. 突出顯示第一列的結果/獲勝突出顯示的單元格中的單元格
- 8. 突出顯示正在搜索的列上的單元格
- 9. 如果單元格不突出顯示
- 10. UITableView在單獨的tableview中顯示搜索結果
- 11. 使用JavaScript在結果中突出顯示搜索關鍵字
- 12. 突出顯示在php錯誤中的搜索結果
- 13. 突出顯示在數據表中的搜索結果
- 14. UITableView:突出顯示最後一個單元格,但其他單元格也突出顯示
- 15. UITableView搜索顯示單元錯誤
- 16. 使用angularJS過濾器在搜索結果中突出顯示搜索文本
- 17. 突出顯示搜索結果中的關鍵字
- 18. 如何突出顯示WPF treeview中的搜索結果?
- 19. 突出顯示搜索結果中的關鍵字php
- 20. 突出顯示CollectionView中的單元格
- 21. 選中時突出顯示單元格
- 22. 在dataGridView的單元格中搜索文本並突出顯示該行?
- 23. 突出顯示搜索TextBlock
- 24. 在Elasticsearch中搜索突出顯示(javascript)
- 25. 突出顯示單個單元格
- 26. 創建單獨的UITableView以顯示搜索結果
- 27. 以編程方式突出顯示UITableView單元格
- 28. VS插件突出顯示搜索結果窗格中的搜索字詞(在文件中查找)
- 29. 使用UISearchBar突出顯示UITableView中的搜索文本
- 30. UITableView搜索顯示的單元格錯誤
你有什麼你到目前爲止看?歸因字符串?顯示您的當前代碼 – Wain
@不,我還沒有做任何事情。但在目標c中看到了屬性字符串的代碼。無法理解..你能幫我這個 –
可能重複[使用UISearchBar在UITableView中突出顯示搜索到的文本](http://stackoverflow.com/questions/7555619/highlight-the-searched-text-in-uitableview -using-uisearchbar) –