danielsaidi / RichTextKit

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

Set colors from RichTextContext #184

Open alelordelo opened 4 months ago

alelordelo commented 4 months ago

I created a func to set colors from RichTextContext:

  // Method to update colors
    public func setColor(_ color: ColorRepresentable, for key: RichTextColor) {
        print("Setting color \(color) for key \(key)")
        colors[key] = color
    }

And then I init like this:

                RichTextEditor(text: richText, context: context, config: editorConfig) { _ in
                  context.setAttributedString(to: cell.richText)
                    context.setColor(.purple, for: .foreground)
                }

Prints: Setting color sRGB IEC61966-2.1 colorspace 0.5 0 0.5 1 for key foreground

But it does not get applied...

Is this correct? Or is there a better way to set a color from the context ?

alelordelo commented 4 months ago

also tried this, but didn't work...

   RichTextEditor(text: richText, context: context, config: editorConfig) { _ in
                  context.setAttributedString(to: cell.richText)
                  context.colors[.foreground] = .purple
                }
danielsaidi commented 4 months ago

Hi @alelordelo - the string needs to be observed in some way for the change to update the UI. You can then use the setColor function in the RichTextContext to update any color.

alelordelo commented 4 months ago

Hi @alelordelo - the string needs to be observed in some way for the change to update the UI. You can then use the setColor function in the RichTextContext to update any color.

thanks @danielsaidi

But isn't that what I am doing here? Or maybe I missed something?

        RichTextEditor(text: richText, context: context, config: editorConfig) { _ in
              context.setAttributedString(to: cell.richText)
                context.setColor(.purple, for: .foreground)
            }