2016-04-27 113 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

相关问题