Skip to content

Commit

Permalink
More text-related APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 1, 2024
1 parent 7d33750 commit 8792b2f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Sources/NSUI/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,11 +42,47 @@ 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()
#else
selectedRange
#endif
}

#if canImport(UIKit)
public var selectedRanges: [NSValue] {
get {
[NSValue(range: selectedRange)]
}
set {
if let range = newValue.first?.rangeValue {
selectedRange = range
}
}
}
#endif
}

0 comments on commit 8792b2f

Please sign in to comment.