diff --git a/Sources/NSUI/TextView.swift b/Sources/NSUI/TextView.swift index 4c771fc..fe1d661 100644 --- a/Sources/NSUI/TextView.swift +++ b/Sources/NSUI/TextView.swift @@ -18,6 +18,20 @@ extension NSTextView { @available(*, deprecated, renamed: "NSTextStorage.EditActions") public typealias NSUITextStorageEditActions = NSTextStorage.EditActions +extension NSLayoutManager { + public func replaceTextStorage(_ newTextStorage: NSTextStorage) { + textStorage?.removeLayoutManager(self) + + for manager in textStorage?.layoutManagers ?? [] { + manager.textStorage = newTextStorage + } + + newTextStorage.addLayoutManager(self) + + textStorage = newTextStorage + } +} + #endif extension NSUITextView { @@ -28,6 +42,29 @@ extension NSUITextView { layoutManager } + /// NSUI wrapper around `textStorage` property. + /// + /// This value can be nil on macOS. + public var nsuiTextStorage: NSTextStorage? { + textStorage + } + + @available(iOS 15.0, macOS 12.0, tvOS 15.0, *) + public var nsuiTextContentStorage: NSTextContentStorage? { +#if canImport(AppKit) && !targetEnvironment(macCatalyst) + textContentStorage +#else + textContainer.textLayoutManager?.textContentManager as? NSTextContentStorage +#endif + } + + /// NSUI wrapper around `textContainer` property. + /// + /// This value can be nil on macOS. + public var nsuiTextContainer: NSTextContainer? { + textContainer + } + public var nsuiSelectedRange: NSRange { #if canImport(AppKit) && !targetEnvironment(macCatalyst) selectedRange() @@ -35,4 +72,17 @@ extension NSUITextView { selectedRange #endif } + +#if canImport(UIKit) + public var selectedRanges: [NSValue] { + get { + [NSValue(range: selectedRange)] + } + set { + if let range = newValue.first?.rangeValue { + selectedRange = range + } + } + } +#endif }