NSRegularExpression,匹配字符串,高亮文本。大约 85% 的时间工作。为什么?
NSRegularExpression, matching strings, and highlighting text. Works about 85% of the time. Why?
我正在处理的应用程序的一部分需要一个字符串,在文本字段中查找它,然后仅突出显示该字符串。它在大约 85% 的时间内都有效。其他 15% 的应用程序表现得好像它突出显示了文本,但文本并未突出显示。没有错误。
字符串(在文本字段中和要比较的字符串中)不可能不同,因为它们来自同一来源。
知道为什么高亮属性没有在应该应用的时候应用吗?
let attributed = NSMutableAttributedString(string: completeText)
var error: NSError?
let regex = NSRegularExpression(pattern: compareString, options: .CaseInsensitive, error: &error)
if let regexError = error {
println("Oh no! \(regexError)")
} else {
println("highlighted")
for match in regex?.matchesInString(completeText, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: count(completeText))) as! [NSTextCheckingResult] {
attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range)
}
}
completeText.text = completeText // the view receiving the text
completeText.attributedText = attributed //apply highlighting
您的最后几行代码令人困惑,您正在将 completeText
分配给 completeText.text
,因此不清楚 completeText
实例是字符串还是 UITextView。您还分配给 .text
和 .attributedText
,这不是必需的。 .text
将在您设置 .attributedText
属性 时自动可用,并且与 attributed.string
具有相同的值。因此只需要设置.attributedText
not.text
.
清理这些东西一切正常。
let completeText = "Hello World, where are you? Where are you?"
let compareString = "Hello World, where are you\? Where are you\?"
let attributed = NSMutableAttributedString(string: completeText)
var error: NSError?
let regex = NSRegularExpression(pattern: compareString, options: .CaseInsensitive, error: &error)
if let regexError = error {
println("Oh no! \(regexError)")
} else {
println("highlighted")
for match in regex?.matchesInString(completeText, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: count(completeText))) as! [NSTextCheckingResult] {
attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range)
}
}
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 400, height: 200))
textView.attributedText = attributed
但是正如您将从我的示例中看到的,如果您的字符串中的某些字符在 RegularExpression 语言中具有特殊含义,例如?、.* 和 [],那么您需要对它们进行转义才能在字符串中找到它们。
请参阅 NSRegularExpression class reference 以获得 table 个特殊字符。
我正在处理的应用程序的一部分需要一个字符串,在文本字段中查找它,然后仅突出显示该字符串。它在大约 85% 的时间内都有效。其他 15% 的应用程序表现得好像它突出显示了文本,但文本并未突出显示。没有错误。
字符串(在文本字段中和要比较的字符串中)不可能不同,因为它们来自同一来源。
知道为什么高亮属性没有在应该应用的时候应用吗?
let attributed = NSMutableAttributedString(string: completeText)
var error: NSError?
let regex = NSRegularExpression(pattern: compareString, options: .CaseInsensitive, error: &error)
if let regexError = error {
println("Oh no! \(regexError)")
} else {
println("highlighted")
for match in regex?.matchesInString(completeText, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: count(completeText))) as! [NSTextCheckingResult] {
attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range)
}
}
completeText.text = completeText // the view receiving the text
completeText.attributedText = attributed //apply highlighting
您的最后几行代码令人困惑,您正在将 completeText
分配给 completeText.text
,因此不清楚 completeText
实例是字符串还是 UITextView。您还分配给 .text
和 .attributedText
,这不是必需的。 .text
将在您设置 .attributedText
属性 时自动可用,并且与 attributed.string
具有相同的值。因此只需要设置.attributedText
not.text
.
清理这些东西一切正常。
let completeText = "Hello World, where are you? Where are you?"
let compareString = "Hello World, where are you\? Where are you\?"
let attributed = NSMutableAttributedString(string: completeText)
var error: NSError?
let regex = NSRegularExpression(pattern: compareString, options: .CaseInsensitive, error: &error)
if let regexError = error {
println("Oh no! \(regexError)")
} else {
println("highlighted")
for match in regex?.matchesInString(completeText, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: count(completeText))) as! [NSTextCheckingResult] {
attributed.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: match.range)
}
}
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 400, height: 200))
textView.attributedText = attributed
但是正如您将从我的示例中看到的,如果您的字符串中的某些字符在 RegularExpression 语言中具有特殊含义,例如?、.* 和 [],那么您需要对它们进行转义才能在字符串中找到它们。
请参阅 NSRegularExpression class reference 以获得 table 个特殊字符。