Post

Replies

Boosts

Views

Activity

Reply to TextKit2: Relation between NSRange and NSTextRange
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) } }
Apr ’24
Reply to TextKit2: Relation between NSRange and NSTextRange
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) } }
Apr ’24