2016-04-27 112 views
0

特定的UITextView文本庫我有一個的UITextField和UIButton的執行搜索:突出顯示的UITextField搜索(swift2)

@IBOutlet weak var searchcodetxt: UITextField! 
@IBOutlet weak var searchcodebtn: UIButton! 

在按下的UIButton,一個函數被調用來搜索的UITextView在UITextFiled所給詞:

@IBAction func searchcode(sender: UIButton) { 
    //searchcodebtn.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside) 
    searchCode() 
} 
func searchCode(){ 
    let keyword = self.searchcodetxt.text 
    let lowercasekeyword = keyword!.lowercaseString 
    let baseString = webcode.text 
    let baselowercase = baseString!.lowercaseString 
    let attributed = NSMutableAttributedString(string: baseString) 
    var error: NSError? 
    do { 
     let regex = try NSRegularExpression(pattern: lowercasekeyword, options: NSRegularExpressionOptions.CaseInsensitive) 
     let matches = regex.matchesInString(baselowercase, options: [], range: NSMakeRange(0, baselowercase.characters.count)) 
     if let match = matches.first { 
      let range = match.rangeAtIndex(1) 
      if let swiftRange = rangeFromNSRange(range, forString: baselowercase) { 
       attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range) 
      } 
     } 
     webcode.attributedText = attributed 
    } catch { 
     // regex was bad! 
     let alertView:UIAlertView = UIAlertView() 
     alertView.title = "Keywords error!" 
     alertView.message = "Please use another keywords" 
     alertView.delegate = self 
     alertView.addButtonWithTitle("OK") 
     alertView.show() 
     // Delay the dismissal by 5 seconds 
     let delay = 5.0 * Double(NSEC_PER_SEC) 
     let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) 
     dispatch_after(time, dispatch_get_main_queue(), { 
      alertView.dismissWithClickedButtonIndex(-1, animated: true) 
     }) 
    } 
} 
func rangeFromNSRange(nsRange: NSRange, forString str: String) -> Range<String.Index>? { 
    let fromUTF16 = str.utf16.startIndex.advancedBy(nsRange.location, limit: str.utf16.endIndex) 
    let toUTF16 = fromUTF16.advancedBy(nsRange.length, limit: str.utf16.endIndex) 
    if let from = String.Index(fromUTF16, within: str), 
     let to = String.Index(toUTF16, within: str) { 
      return from ..< to 
    } 
    return nil 
} 

但是,當我點擊搜索按鈕時,它沒有給特定的單詞着色。如何糾正?

回答

0

你的代碼需要修改才能使其工作。

searchCode()函數中,let range = match.rangeAtIndex(1)將導致索引超出限制的應用程序崩潰。

您可以簡單地使用match.range添加屬性:

if let match = matches.first { 

     attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range) 
} 

只是糾正這一行將會使你的代碼的工作,但將重點只在文本視圖中的第一場比賽。

強調所有的比賽,您可以更改此塊

if let match = matches.first { 
     let range = match.rangeAtIndex(1) 
     if let swiftRange = rangeFromNSRange(range, forString: baselowercase) { 
      attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range) 
     } 
    } 

,並添加匹配數組一個循環,在所有匹配迭代,並添加一個屬性突出他們。

for match in matches { 
    attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range) 
} 

所以完整代碼searchCode()將是:現在

func searchCode(){ 
    let keyword = self.searchcodetxt.text 
    let lowercasekeyword = keyword!.lowercaseString 
    let baseString = webcode.text 
    let baselowercase = baseString!.lowercaseString 
    let attributed = NSMutableAttributedString(string: baseString) 

    do { 
     let regex = try NSRegularExpression(pattern: lowercasekeyword, options: NSRegularExpressionOptions.CaseInsensitive) 
     let matches = regex.matchesInString(baselowercase, options: [], range: NSMakeRange(0, baselowercase.characters.count)) 

     for match in matches { 
      attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range) 
     } 

     webcode.attributedText = attributed 
    } catch { 
     // regex was bad! 
     let alertView:UIAlertView = UIAlertView() 
     alertView.title = "Keywords error!" 
     alertView.message = "Please use another keywords" 
     alertView.delegate = self 
     alertView.addButtonWithTitle("OK") 
     alertView.show() 
     // Delay the dismissal by 5 seconds 
     let delay = 5.0 * Double(NSEC_PER_SEC) 
     let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) 
     dispatch_after(time, dispatch_get_main_queue(), { 
      alertView.dismissWithClickedButtonIndex(-1, animated: true) 
     }) 
    } 
} 

,你就不需要rangeFromNSRange()方法。

+0

這是否幫助您或您有任何其他查詢? – UditS

+0

這個答案是正確的。綠色的你。謝謝你,朋友。 – Crocodile

相關問題