【问题标题】:Highlight specific UITextView text base on UITextField search (swift2)基于 UITextField 搜索(swift2)突出显示特定的 UITextView 文本
【发布时间】:2016-08-21 08:19:10
【问题描述】:

我有一个 UITextField 和 UIButton 来执行搜索:

@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
}

但是当我点击搜索按钮时,它并没有为特定的单词着色。如何纠正这个问题?

【问题讨论】:

    标签: ios uibutton swift2 uitextfield uitextview


    【解决方案1】:

    您的代码需要进行一些更改才能使其正常工作。

    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)
            }
        }
    

    并在matches数组上添加一个循环,遍历所有匹配项并添加一个属性以突出显示它们。

    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() 方法。

    【讨论】:

    • 这对您有帮助还是您有任何其他疑问?
    • 这个答案是正确的。为你绿。谢谢你,朋友。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2017-12-11
    • 2023-04-08
    • 2018-09-29
    相关资源
    最近更新 更多