ContentView.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | // // ContentView.swift // SwiftUIProject // // Created by Cairocoders // import SwiftUI struct ContentView: View { @State private var text: String = "" @State private var color: Color = Color.indigo @State private var count: Int = 0 var body: some View { ZStack { HStack { TextField( "Enter Text" , text: $text) .onChange(of: text) { text in let letters = text.trimmingCharacters(in: .whitespaces).count self.count = letters switch letters { case 1..<10: self.color = .red case 10..<20: self.color = .blue case 20...: self.color = .green default : self.color = .indigo } } .foregroundColor(color) Text( "\(count)" ) .bold() .foregroundColor(.gray) } .padding() .overlay( RoundedRectangle(cornerRadius: 30) .stroke(color, lineWidth: 5) ) .padding() } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |