我有一个在深色背景上显示浅色文本的视图。我需要为其添加一个上下文菜单。这是我正在使用的代码:
Text(rowModel.detail)
.font(detailFont)
.foregroundColor(Color(/* a light color */))
.contextMenu {
Button(action: {
UIPasteboard.general.string = self.rowModel.detail
}, label: {
Text("Copy")
})
}
Run Code Online (Sandbox Code Playgroud)
当菜单被激活并且设备处于灯光模式时,结果如下:
我在网上找不到任何表明我可以更改突出显示颜色的信息。但为了以防万一,我想我会在这里问。能做到吗?谢谢!
您可以执行此操作来为按钮设置自定义突出显示状态
struct YourButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding()
.foregroundColor(.white)
.background(configuration.isPressed ? Color.green : Color.blue)
.cornerRadius(8.0)
}
}
// To use it
Button(action: {
UIPasteboard.general.string = self.rowModel.detail
}, label: {
Text("Copy")
})
.buttonStyle(YourButtonStyle())
Run Code Online (Sandbox Code Playgroud)