使用 Swift 从 NSString 中查找 String 作为 NSRange 的出现,结果 NSRange 将在 NSAttributedString 中使用
Finding occurrences of String as NSRange from an NSString using Swift, result NSRange to be used in NSAttributedString
我正在尝试在一个字符串中查找 NSRange
的多个字符串。
在我下面的代码中,我使用 String.range(of: String, options: , Range)
方法找到 Range
,并转换为 NSRange
。当文本包含多代码单元 unicode 字符时,此转换失败,例如表情符号:
let findInString = "This #is a #tag #tag inten#sive#search" // MAY CONTAINS EMOJIS
let findStrings = ["#is","#tag","#sive","#search"]
let result = NSMutableAttributedString(string: findInString)
for (index, stringToFind) in findStrings.enumerated() {
var nextStartIndex = findInString.startIndex
while let range = findInString.range(of: stringToFind, options: [.literal, .caseInsensitive], range: nextStartIndex..<findInString.endIndex) {
let start = findInString.distance(from: findInString.startIndex, to: range.lowerBound)
let length = findInString.distance(from: range.lowerBound, to: range.upperBound)
result.addAttribute(NSLinkAttributeName, value: "\(index):", range: NSMakeRange(start, length))
nextStartIndex = range.upperBound
}
}
问题:是否有效如果我使用NSString.range()
查找NSRange
。我正在尝试这个,但是我的以下代码在 range:
部分有错误。
let findInNsString = findInString as NSString
while let range = findInNsString.range(of: stringToFind, options: [.literal, .caseInsensitive], range: nextStartIndex..<findInString.endIndex)
我需要帮助来理解和更正上述错误,在此先感谢。
找到了将 Range
转换为 NSRange
的正确方法,感谢 MartinR 提供的 answer
我使用错误的方式将 Range
转换为 NSRange
,这里是工作代码片段,其中包含从 Range
转换为 NSRange
的正确方法:
let findStrings = ["#is","#tag","#siØve","#search"]
let findInString = "This #is a #tag #tag inten#siØve#search"
let result = NSMutableAttributedString(string: findInString)
let utf16 = findInString.utf16
for (index, stringToFind) in findStrings.enumerated() {
var nextStartIndex = findInString.startIndex
while let range = findInString.range(of: stringToFind, options: [.literal, .caseInsensitive], range: nextStartIndex..<findInString.endIndex) {
// PROPER WAY TO CONVERT TO NSRange
let from = range.lowerBound.samePosition(in: utf16)
let start = utf16.distance(from: utf16.startIndex, to: from)
let length = utf16.distance(from: from, to: range.upperBound.samePosition(in: utf16))
result.addAttribute(NSLinkAttributeName, value: "\(index):", range: NSMakeRange(start, length))
nextStartIndex = range.upperBound
}
}
我正在尝试在一个字符串中查找 NSRange
的多个字符串。
在我下面的代码中,我使用 String.range(of: String, options: , Range)
方法找到 Range
,并转换为 NSRange
。当文本包含多代码单元 unicode 字符时,此转换失败,例如表情符号:
let findInString = "This #is a #tag #tag inten#sive#search" // MAY CONTAINS EMOJIS
let findStrings = ["#is","#tag","#sive","#search"]
let result = NSMutableAttributedString(string: findInString)
for (index, stringToFind) in findStrings.enumerated() {
var nextStartIndex = findInString.startIndex
while let range = findInString.range(of: stringToFind, options: [.literal, .caseInsensitive], range: nextStartIndex..<findInString.endIndex) {
let start = findInString.distance(from: findInString.startIndex, to: range.lowerBound)
let length = findInString.distance(from: range.lowerBound, to: range.upperBound)
result.addAttribute(NSLinkAttributeName, value: "\(index):", range: NSMakeRange(start, length))
nextStartIndex = range.upperBound
}
}
问题:是否有效如果我使用NSString.range()
查找NSRange
。我正在尝试这个,但是我的以下代码在 range:
部分有错误。
let findInNsString = findInString as NSString
while let range = findInNsString.range(of: stringToFind, options: [.literal, .caseInsensitive], range: nextStartIndex..<findInString.endIndex)
我需要帮助来理解和更正上述错误,在此先感谢。
找到了将 Range
转换为 NSRange
的正确方法,感谢 MartinR 提供的 answer
我使用错误的方式将 Range
转换为 NSRange
,这里是工作代码片段,其中包含从 Range
转换为 NSRange
的正确方法:
let findStrings = ["#is","#tag","#siØve","#search"]
let findInString = "This #is a #tag #tag inten#siØve#search"
let result = NSMutableAttributedString(string: findInString)
let utf16 = findInString.utf16
for (index, stringToFind) in findStrings.enumerated() {
var nextStartIndex = findInString.startIndex
while let range = findInString.range(of: stringToFind, options: [.literal, .caseInsensitive], range: nextStartIndex..<findInString.endIndex) {
// PROPER WAY TO CONVERT TO NSRange
let from = range.lowerBound.samePosition(in: utf16)
let start = utf16.distance(from: utf16.startIndex, to: from)
let length = utf16.distance(from: from, to: range.upperBound.samePosition(in: utf16))
result.addAttribute(NSLinkAttributeName, value: "\(index):", range: NSMakeRange(start, length))
nextStartIndex = range.upperBound
}
}