问题
Can I override a Menu button label's "post-set dimmed" color?
The GIF below shows a legibly bright menu item that dims after new selection. (Default behavior for this system style (e.g., in trackpad prefs).
But it fails accessibility standards, such as WCAG's requirement for > 4.5 : 1 luminance contrast for that font size in an active control (system default is ~ 2).
I've tried:
- setting
accentColor
andforegroundColor
everywhere - using
onChange
to update an @State color fed into the aforementioned modifiers - using
onReceive
forNSMenu.didSendActionNotification
or from calls to the delegatemenuDidClose
method (was hunting for some appearance setting) - using the
DefaultMenuStyle
, not borderless - ramping up
brightness
orcontrast
, but that introduces aliasing issues
Any suggestions? Maybe I'm just supposed to be using a Picker? Should I flag this as an accessibility issue with Apple?
struct BareExample: View {
var body: some View {
Menu { menuContents }
label: { menuLabel }
.menuStyle(BorderlessButtonMenuStyle())
.fixedSize()
}
var menuContents: some View {
ForEach(sortOptions) { option in
Button(option.rawValue) { setSortOrder(option) }
.accentColor(labelColor)
.foregroundColor(labelColor)
}
}
var menuLabel: some View {
HStack {
Image(systemName: sortIcon)
Text(sortOrderLabel)
}
.accentColor(labelColor)
.foregroundColor(labelColor)
}
@State var sortOrder: PaletteSortOrder = .sequential
var sortOrderLabel: String { sortOrder.rawValue }
let sortIcon = "arrow.up.arrow.down"
let sortOptions = PaletteSortOrder.allCases
@State var labelColor = Color.white
func setSortOrder(_ order: PaletteSortOrder) {
sortOrder = order
labelColor = Color.white
}
}
public enum PaletteSortOrder: String, CaseIterable, Identifiable {
case sequential = "Sequential"
case byLuminance = "Luminance"
case byWorstPairwiseContrast = "Pairwise Contrast"
public var id: String { rawValue }
}
来源:https://stackoverflow.com/questions/65771764/override-menu-button-label-text-color-macos-swiftui