在 NSTextView 中查找子字符串,然后滚动到子字符串的位置

Find a substring within a NSTextView and then scroll to the substring's location

我正在构建一个简单的 Mac 应用程序,它有一个带有非常长字符串(15mb 文本)的 NSTextView。我想找到一个特定的子字符串(时间戳),然后向下滚动到子字符串在 NSTextView 中的位置。

现在我可以找到子字符串所在位置的索引,但它的格式很奇怪。我在这里使用了最佳答案:

@IBOutlet weak var logTextView: NSScrollView!
@IBOutlet var logTextBox: NSTextView!

let searchString = "2018-03-29 19:10:17"
let baseString = logTextBox.string

let findIndex = baseString.endIndex(of: searchString)!
print(findIndex)


// Function I got from the stack overflow link
extension StringProtocol where Index == String.Index {
func index<T: StringProtocol>(of string: T, options: String.CompareOptions = []) -> Index? {
    return range(of: string, options: options)?.lowerBound
}
func endIndex<T: StringProtocol>(of string: T, options: String.CompareOptions = []) -> Index? {
    return range(of: string, options: options)?.upperBound
}
func indexes<T: StringProtocol>(of string: T, options: String.CompareOptions = []) -> [Index] {
    var result: [Index] = []
    var start = startIndex
    while start < endIndex, let range = range(of: string, options: options, range: start..<endIndex) {
        result.append(range.lowerBound)
        start = range.lowerBound < range.upperBound ? range.upperBound : index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
    }
    return result
}
func ranges<T: StringProtocol>(of string: T, options: String.CompareOptions = []) -> [Range<Index>] {
    var result: [Range<Index>] = []
    var start = startIndex
    while start < endIndex, let range = range(of: string, options: options, range: start..<endIndex) {
        result.append(range)
        start = range.lowerBound < range.upperBound  ? range.upperBound : index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
    }
    return result
}
}

NSTextViewNSText的子类,NSText定义了一个scrollRangeToVisible(NSRange)方法。所以你可以将它用于滚动部分,但它需要一个 NSRange.

有一个记录不完整的 NSRange 初始化程序可以将 Range 转换为 NSRange。所以:

let haystack = logTextBox.string
let needle = "2018-03-29 19:10:17"
if let needleRange = haystack.range(of: needle) {
    logTextBox.scrollRangeToVisible(NSRange(needleRange, in: haystack))
}