1

I'm utilizing Daniel Saidi's RichTextKit in my app to allow for rich text editing. https://github.com/danielsaidi/RichTextKit

I am having trouble updating

RichTextEditor(text: $value1, context: value2)

programmatically via a function. I'm wanting the ability to insert text, but whenever I try

$value1.wrappedValue = "New value"

It doesn't update the RichTextEditor with anything new. If I call print($value1.wrappedValue) the value has updated there, but it won't do anything on the TextEditor itself.

Is there a way to update bindings AND have them update the view?

  • Are you sure you are updating the same binding? I think you'll need to show more code. – Giles Oct 13 '22 at 10:23
  • Keep it simple if you are just starting with SwiftUI, see what SwiftUI provides out of the box, it has AttributedText. TextEditor even supports markdown, see all possibilities before using 3rd party library which adds another dependancy to your code – user1046037 Oct 13 '22 at 17:07
  • Bindings don't serve as a dependancy, so change in binding value will not refresh the view. You could use `onChange(of:perform:)` on the view to update a `@State` property which would refresh the view – user1046037 Oct 13 '22 at 17:09

1 Answers1

0

I had the same trouble...but then found the following function in the API:

https://danielsaidi.github.io/RichTextKit/documentation/richtextkit/richtextcontext/shouldsetattributedstring

context.shouldSetAttributedString = updatedAttributedString

As others mentioned, updating the binding directly won't cause a view refresh, but updating via that context method did.

Hope that helps!

Joel
  • 423
  • 1
  • 6
  • 14