Override menu button label text color (MacOS SwiftUI)

我与影子孤独终老i 提交于 2021-02-11 14:23:34

问题


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 and foregroundColor everywhere
  • using onChange to update an @State color fed into the aforementioned modifiers
  • using onReceive for NSMenu.didSendActionNotification or from calls to the delegate menuDidClose method (was hunting for some appearance setting)
  • using the DefaultMenuStyle, not borderless
  • ramping up brightness or contrast, 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!