danielsaidi / RichTextKit

RichTextKit is a Swift SDK that helps you use rich text in Swift and SwiftUI.
MIT License
944 stars 126 forks source link

Make is possible to replace text at range #47

Closed danielsaidi closed 1 month ago

danielsaidi commented 1 year ago

We can currently use the context to select a range, but not replace the currently selected text.

This should be possible when replacing text.

Replace the pasteText function with an insertText(at: position) (non-replacing) and insertText(at: range) (replacing).

savanteer commented 1 year ago

Thank you again for your truly impressive library. What a gift!

It appears to already be possible (early Aug '23) to replace text at a specific offset:

context.pasteText("<some text>", at: <some offset>, moveCursorToPastedContent: T/F)

But yes, a range would definitely be more usable. However, the solution should support styled text (ie, NSAttributedString) as well. For example:

let attr = [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24]
let text = NSMutableAttributedString(string: "<some text here>\n", attributes: attr)

<magic>.replaceText(in: context.selectedRange, with: text)

FWIW, pasteText() does not appear to accommodate styled text.

danielsaidi commented 1 year ago

Hi @savanteer

Thanks for your feedback! I hope to have time to work on this in a few weeks.

danielsaidi commented 1 month ago

I have added an action-based way of doing this.

It's merged into main and will be available in 1.1.