Skip to content

Updated the possibility to use viewModifiers to change the background Color#70

Closed
holgerkrupp wants to merge 3 commits intoNuPlay:mainfrom
holgerkrupp:main
Closed

Updated the possibility to use viewModifiers to change the background Color#70
holgerkrupp wants to merge 3 commits intoNuPlay:mainfrom
holgerkrupp:main

Conversation

@holgerkrupp
Copy link
Copy Markdown

@holgerkrupp holgerkrupp commented Aug 7, 2025

RichText(html: HTMLCONTENT)
.richTextBackground(.clear)

Holger Krupp added 2 commits August 7, 2025 20:14
…sing - not the optimal solution but a quick fix to get the possibility to add transparent backgrounds)
@jcovin293
Copy link
Copy Markdown
Contributor

jcovin293 commented Aug 7, 2025

This looks like a good solution to the issue! From my knowledge of the codebase from my previous PRs, lots of the modifiers and config settings just use custom CSS under the hood.

Side note: I would recommend making the cssString method private because (from my understanding) it is a helper method that isn't intended to be used by users.

@holgerkrupp
Copy link
Copy Markdown
Author

Absolutely right. That method should be private.

@phamdinhduc795397
Copy link
Copy Markdown

It would be best if the content had a clear background so that the user can set the color via the background modifier.

RichText(html: HTMLCONTENT)
.background(Color.red)

@NuPlay NuPlay requested a review from Copilot August 17, 2025 12:22
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for customizing the background color of RichText components using a SwiftUI view modifier. The implementation allows developers to set a custom background color via .richTextBackground(.clear) or any other Color value, which gets converted to appropriate CSS styling.

Key changes:

  • Adds a new richTextBackgroundModifier with color-to-CSS conversion
  • Extends Configuration to support custom background colors
  • Implements cross-platform color handling for both UIKit and AppKit

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
RichTextBackgroundModifier.swift New view modifier for setting background colors with CSS conversion
Configuration.swift Adds backgroundColor property and initializer to support custom backgrounds

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

}
}

public init(backgroundColor: String?) {
Copy link

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This convenience initializer duplicates the main initializer's parameters with hardcoded defaults, creating maintenance overhead. Consider using default parameter values in the main initializer instead, or make this initializer call the main one with only the backgroundColor parameter being different.

Copilot uses AI. Check for mistakes.
// Add or update background-color in customCSS
if result.configuration.customCSS.contains("background-color:") {
// Replace previous background-color if it exists
result.configuration.customCSS = result.configuration.customCSS.replacingOccurrences(of: #"background-color:.*?;"#, with: "background-color: \(cssColor);", options: .regularExpression)
Copy link

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern background-color:.*?; may match incomplete CSS rules or interfere with other properties that contain 'background-color' as a substring. Consider using a more specific pattern like background-color:\s*[^;]*; to match the complete property more accurately.

Suggested change
result.configuration.customCSS = result.configuration.customCSS.replacingOccurrences(of: #"background-color:.*?;"#, with: "background-color: \(cssColor);", options: .regularExpression)
result.configuration.customCSS = result.configuration.customCSS.replacingOccurrences(of: #"background-color:\s*[^;]*;"#, with: "background-color: \(cssColor);", options: .regularExpression)

Copilot uses AI. Check for mistakes.
let red = Int((nsColor.redComponent * 255).rounded())
let green = Int((nsColor.greenComponent * 255).rounded())
let blue = Int((nsColor.blueComponent * 255).rounded())
return String(format: "#%02lX%02lX%02lX", red, green, blue)
Copy link

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format specifier %02lX is incorrect for Int values. Use %02X instead, as %02lX expects long values but Int values are being passed.

Suggested change
return String(format: "#%02lX%02lX%02lX", red, green, blue)
return String(format: "#%02X%02X%02X", Int(red * 255), Int(green * 255), Int(blue * 255))
#else
if color == .clear { return "transparent" }
let nsColor = NSColor(color)
let red = Int((nsColor.redComponent * 255).rounded())
let green = Int((nsColor.greenComponent * 255).rounded())
let blue = Int((nsColor.blueComponent * 255).rounded())
return String(format: "#%02X%02X%02X", red, green, blue)

Copilot uses AI. Check for mistakes.
let red = Int((nsColor.redComponent * 255).rounded())
let green = Int((nsColor.greenComponent * 255).rounded())
let blue = Int((nsColor.blueComponent * 255).rounded())
return String(format: "#%02lX%02lX%02lX", red, green, blue)
Copy link

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format specifier %02lX is incorrect for Int values. Use %02X instead, as %02lX expects long values but Int values are being passed.

Suggested change
return String(format: "#%02lX%02lX%02lX", red, green, blue)
return String(format: "#%02X%02X%02X", Int(red * 255), Int(green * 255), Int(blue * 255))
#else
if color == .clear { return "transparent" }
let nsColor = NSColor(color)
let red = Int((nsColor.redComponent * 255).rounded())
let green = Int((nsColor.greenComponent * 255).rounded())
let blue = Int((nsColor.blueComponent * 255).rounded())
return String(format: "#%02X%02X%02X", red, green, blue)

Copilot uses AI. Check for mistakes.
@NuPlay
Copy link
Copy Markdown
Owner

NuPlay commented Aug 17, 2025

Thank you for fixing the issue(#68) and creating this PR 🙏
Sorry I couldn’t review it sooner.

I really appreciate your contribution again. If you could just address the points suggested by Copilot, I’ll go ahead and merge it right away.
If you have any other thoughts or suggestions, please feel free to share them.

Thanks again!

@NuPlay NuPlay mentioned this pull request Aug 24, 2025
10 tasks
@NuPlay
Copy link
Copy Markdown
Owner

NuPlay commented Aug 24, 2025

Hi @holgerkrupp,
Thank you so much for taking the time to contribute this PR! I really appreciate your effort and the improvements you suggested.

I wanted to let you know that with the recent release of v3.0.0, we've addressed the underlying issue in a different way as part of a broader refactoring. As a result, this PR is no longer needed.
I apologize for not being able to merge your contribution, and I'm sorry if this caused any inconvenience. Your willingness to contribute to the project means a lot, and I hope you'll consider contributing again in the future.

Thanks again for your understanding and support!
Closing this PR as it's been resolved in v3.0.0.

@NuPlay NuPlay closed this Aug 24, 2025
@holgerkrupp
Copy link
Copy Markdown
Author

Hi,

No problem. I wasn't 100% happy with my solution anyway and wanted to rewrite it but I'm currently working on a new app that should be ready for iOS26 (that's the reason why I didn't address the copilot comments). I'll have a look at your new version soon 👍🏻

독일에서 인사드립니다,
Holger

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

Successfully merging this pull request may close these issues.

5 participants