Or you could extend NSTextContentManager:
extension NSTextContentManager {
func range(for textRange: NSTextRange) -> NSRange? {
let location = offset(from: documentRange.location, to: textRange.location)
let length = offset(from: textRange.location, to: textRange.endLocation)
if location == NSNotFound || length == NSNotFound { return nil }
return NSRange(location: location, length: length)
}
func textRange(for range: NSRange) -> NSTextRange? {
guard let textRangeLocation = location(documentRange.location, offsetBy: range.location),
let endLocation = location(textRangeLocation, offsetBy: range.length) else { return nil }
return NSTextRange(location: textRangeLocation, end: endLocation)
}
}
Topic:
App & System Services
SubTopic:
General
Tags: