2015-11-05 181 views
2

我在TableView中實現了一個搜索欄。現在我想強調結果。例如,如果我輸入了兩個字母,那麼這兩個字母應該在從搜索欄下拉的結果TableView中突出顯示。任何人都可以幫助我做到這一點?我知道我應該使用自定義單元格,但我失去了實現它。在UITableView單元格中突出顯示搜索結果iOS Swift

+2

你有什麼你到目前爲止看?歸因字符串?顯示您的當前代碼 – Wain

+0

@不,我還沒有做任何事情。但在目標c中看到了屬性字符串的代碼。無法理解..你能幫我這個 –

+0

可能重複[使用UISearchBar在UITableView中突出顯示搜索到的文本](http://stackoverflow.com/questions/7555619/highlight-the-searched-text-in-uitableview -using-uisearchbar) –

回答

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!) 
      } 
     ) 
+0

你可以在swift中發佈代碼 –

+0

讓我來提供。 – Vijay

+0

@GaneshKumar編輯我的答案。 – Vijay

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 
相關問題