iosxcodeswiftuimenucontextmenu

How to change the background color of the selected text from clear to black when contextMenu is active?


Simulator

#this is just an example of the code so when i longpress the "Text("Long press for menu")" and is showing the contextmenu to change the background color:

 struct ContentView: View {
   var body: some View {
        Text("Long press for menu")
            .contextMenu {
                Button {
                    print("Pills selected")
                } label: {
                    Label("Pills", systemImage: "pills")
                }

                Button {
                    print("Heart selected")
                } label: {
                    Label("Heart rate", systemImage: "heart")
                }

                Button {
                    print("ECG selected")
                } label: {
                    Label("ECG", systemImage: "waveform.path.ecg")
                }
            }
    }
}```

Solution

  • Thanks @Gregorian.st it didn't seem so hard at all. I just wanted to have two different colors one when preview is true and one when preview is false and to have a great transition when the background colors are changing. Cheers

    import SwiftUI
    
    struct SideMenuConversationIds: View {
        @State private var isPreview: Bool = false
    
        var body: some View {
            TextView(isPreview: isPreview)
                .contextMenu {
                    Button {
                        print("Pills selected")
                    } label: {
                        Label("Pills", systemImage: "pills")
                    }
    
                    Button {
                        print("Heart selected")
                    } label: {
                        Label("Heart rate", systemImage: "heart")
                    }
    
                    Button {
                        print("ECG selected")
                    } label: {
                        Label("ECG", systemImage: "waveform.path.ecg")
                    }
                } preview: {
                    TextView(isPreview: true)
                        .onAppear {
                            isPreview = true
                        }
                        .onDisappear {
                            isPreview = false
                        }
                }
        }
    }
    
    struct TextView: View {
        var isPreview: Bool = false
    
        var body: some View {
            Text("Long press for menu")
                .padding()
                .foregroundColor(.black)
                .background(AnimatedBackgroundColor(isPreview: isPreview))
                .cornerRadius(30)
        }
    }
    
    struct AnimatedBackgroundColor: View {
        var isPreview: Bool
        
        @State private var backgroundColor: Color = .white
    
        var body: some View {
            Rectangle()
                .fill(backgroundColor)
                .onAppear {
                    withAnimation(.easeInOut(duration: 0.1)) {
                        backgroundColor = isPreview ? .yellow : .white
                    }
                }
                .onChange(of: isPreview) { newValue in
                    withAnimation(.easeInOut(duration: 0.1)) {
                        backgroundColor = newValue ? .yellow : .white
                    }
                }
        }
    }