Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichTextEditorStyle not supporting colorScheme #215

Open
RizwanaDesai opened this issue Nov 25, 2024 · 3 comments
Open

RichTextEditorStyle not supporting colorScheme #215

RizwanaDesai opened this issue Nov 25, 2024 · 3 comments

Comments

@RizwanaDesai
Copy link

I'm using RichTextEditorStyle to set up different color and font for editor using below code

 private var editorStyle: RichTextEditorStyle {
        let nsFont = NSFont(name: "New York", size: 16) ?? NSFont.systemFont(ofSize: 16)
        return RichTextEditorStyle(
            font: nsFont,
            fontColor: .textPrimary
        )
    }
RichTextEditor(text: $selectedSection.richText, context: context)
        .richTextEditorStyle(editorStyle)

Here .textPrimary is added in color assets for both dark and light mode, but it's not dynamically adapting to light and dark mode, and when i reopen a file it's using color from AnyAppearance for font instead of using color according to colorScheme(light or dark)

I would really appreciate any help for this.

@karlmira
Copy link

karlmira commented Dec 15, 2024

HI,

I have the same problem. I found out that the text color is in the formatting of the NSAttributedString. This means that you always have to adjust the color when changing the color scheme.

Be careful, though, if you have already formatted individual elements in the text in color. And when printing, you have to reset the textColor back to black or whatever you need for printing.

But you can do:

@Environment(\.colorScheme) private var colorScheme: ColorScheme

...

RichTextEditor(text: $selectedSection.richText, context: context)
		.onChange(of: colorScheme) { _, _ in
			applyRemoveTextColor()
		}

...

	private func applyRemoveTextColor() {
		let attributedString = context.attributedString
		let updatedString = removeTextColor(from: attributedString, color: colorScheme == .dark ? .white : .black)
		context.setAttributedString(to: updatedString)
	}


@karlmira
Copy link

Forgot a func you need.

import Foundation
import AppKit

func removeTextColor(from attributedString: NSAttributedString, color: NSColor) -> NSAttributedString {
	let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
	
	mutableAttributedString.enumerateAttribute(.foregroundColor, in: NSRange(location: 0, length: mutableAttributedString.length)) { _, range, _ in
		mutableAttributedString.removeAttribute(.foregroundColor, range: range)
		mutableAttributedString.addAttribute(.foregroundColor, value: color, range: range)
	}
	
	return mutableAttributedString as NSAttributedString
}

@danielsaidi
Copy link
Owner

Thank you @karlmira 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants