extension NSRange {
init?(textRange: NSTextRange, in contentManager: NSTextContentManager) {
let location = contentManager.offset(from: contentManager.documentRange.location, to: textRange.location)
let length = contentManager.offset(from: textRange.location, to: textRange.endLocation)
if location == NSNotFound || length == NSNotFound { return nil }
self.init(location: location, length: length)
}
}
extension NSTextRange {
convenience init?(range: NSRange, in contentManager: NSTextContentManager) {
guard let location = contentManager.location(contentManager.documentRange.location, offsetBy: range.location),
let endLocation = contentManager.location(location, offsetBy: range.length) else { return nil }
self.init(location: location, end: endLocation)
}
}
Topic:
App & System Services
SubTopic:
General
Tags: