Swift/SwiftUI - Unable to update binding value programmatically for RichTextKit's RichTextContext

385 Views Asked by At

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?

1

There are 1 best solutions below

0
Joel On

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!