Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created February 15, 2022 18:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/057676af670f06fa6061473ac28a6c58 to your computer and use it in GitHub Desktop.
Save krzyzanowskim/057676af670f06fa6061473ac28a6c58 to your computer and use it in GitHub Desktop.
NSRange TextKit2 helpers
extension NSRange {
static let notFound = NSRange(location: NSNotFound, length: 0)
var isEmpty: Bool {
length == 0
}
init(_ textRange: NSTextRange, in textContentManager: NSTextContentManager) {
let offset = textContentManager.offset(from: textContentManager.documentRange.location, to: textRange.location)
let length = textContentManager.offset(from: textRange.location, to: textRange.endLocation)
self.init(location: offset, length: length)
}
init(_ textLocation: NSTextLocation, in textContentManager: NSTextContentManager) {
let offset = textContentManager.offset(from: textContentManager.documentRange.location, to: textLocation)
self.init(location: offset, length: 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment