Updated the possibility to use viewModifiers to change the background Color#70
Updated the possibility to use viewModifiers to change the background Color#70holgerkrupp wants to merge 3 commits intoNuPlay:mainfrom
Conversation
…sing - not the optimal solution but a quick fix to get the possibility to add transparent backgrounds)
|
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. |
|
Absolutely right. That method should be private. |
|
It would be best if the content had a clear background so that the user can set the color via the background modifier. |
There was a problem hiding this comment.
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
richTextBackgroundModifierwith color-to-CSS conversion - Extends
Configurationto 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?) { |
There was a problem hiding this comment.
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.
| // 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) |
There was a problem hiding this comment.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
The format specifier %02lX is incorrect for Int values. Use %02X instead, as %02lX expects long values but Int values are being passed.
| 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) |
| 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) |
There was a problem hiding this comment.
The format specifier %02lX is incorrect for Int values. Use %02X instead, as %02lX expects long values but Int values are being passed.
| 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) |
|
Thank you for fixing the issue(#68) and creating this PR 🙏 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. Thanks again! |
|
Hi @holgerkrupp, 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. Thanks again for your understanding and support! |
|
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 👍🏻 독일에서 인사드립니다, |
RichText(html: HTMLCONTENT)
.richTextBackground(.clear)