Advertisement
QwertyAvatar

Untitled

Apr 4th, 2023 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.33 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct ContentView: View {
  4.     @State var textColor = Color.black
  5.    
  6.     var body: some View {
  7.         NavigationView {
  8.             VStack {
  9.                 Text("Witaj w mojej aplikacji!")
  10.                     .foregroundColor(textColor)
  11.                 NavigationLink(
  12.                     destination: ColorSelectionView(textColor: $textColor),
  13.                     label: {
  14.                         Text("Wybierz kolor")
  15.                             .foregroundColor(.white)
  16.                             .padding()
  17.                             .background(Color.blue)
  18.                             .cornerRadius(10)
  19.                     })
  20.             }
  21.             .navigationTitle("Aplikacja z kolorem")
  22.         }
  23.     }
  24. }
  25.  
  26. struct ContentView_Previews: PreviewProvider {
  27.     static var previews: some View {
  28.         ContentView()
  29.     }
  30. }
  31.  
  32.  
  33. =============
  34.  
  35. import SwiftUI
  36.  
  37. struct ColorSelectionView: View {
  38.     @Binding var textColor: Color
  39.    
  40.     var body: some View {
  41.         VStack {
  42.             Text("Wybierz kolor tekstu:")
  43.             HStack {
  44.                 ColorBox(color: .red, textColor: $textColor)
  45.                 ColorBox(color: .green, textColor: $textColor)
  46.             }
  47.             HStack {
  48.                 ColorBox(color: .blue, textColor: $textColor)
  49.                 ColorBox(color: .yellow, textColor: $textColor)
  50.             }
  51.         }
  52.         .navigationTitle("Wybór koloru")
  53.     }
  54. }
  55.  
  56. struct ColorSelectionView_Previews: PreviewProvider {
  57.     static var previews: some View {
  58.         ColorSelectionView(textColor: .constant(Color.black))
  59.     }
  60. }
  61.  
  62.  
  63. =================
  64.  
  65. import SwiftUI
  66.  
  67. struct ColorBox: View {
  68.     let color: Color
  69.     @Binding var textColor: Color
  70.    
  71.     var body: some View {
  72.         Button(action: {
  73.             textColor = color
  74.         }, label: {
  75.             RoundedRectangle(cornerRadius: 10)
  76.                 .fill(color)
  77.                 .frame(width: 50, height: 50)
  78.                 .overlay(
  79.                     RoundedRectangle(cornerRadius: 10)
  80.                         .stroke(textColor == color ? Color.white : Color.clear, lineWidth: 4)
  81.                 )
  82.         })
  83.     }
  84. }
  85.  
  86. struct ColorBox_Previews: PreviewProvider {
  87.     static var previews: some View {
  88.         ColorBox(color: .red, textColor: .constant(Color.black))
  89.     }
  90. }
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement