TextKit2: Relation between NSRange and NSTextRange

The way NSTextView is built it's inevitable to use NSTextStorage with TextKit2, however the NSAttributedString uses NSRange vs the TextKit2 family uses NSTextRange for text location, etc. What I struggle with is the relation between these two. I didn't find a convenient translation between these two. Is NSAttributedStrint NSRange length=1 equal to NSTextRange offset 1? I think it's not (at least it's not necessarily true for every NSTextContentManager.

So my question is, given a NSTextRange, what is the corresponding NSRange in NSTextContentStorage.attributedString

Replies

I figured out a clumsy way to convert NSTextRange to NSRange, of cause by researching the development documents for ages.

let offset = textContentStorage!.offset(from: textLayoutManager!.documentRange.location, to: textRange.location)
let length = textContentStorage!.offset(from: textRange.location, to: textRange.endLocation)
let nsRange = NSRange(location: offset, length: length)