@@ -2,15 +2,48 @@ import SwiftUI
22
33/// Text field has a rounded border that has a thicker border and brighter border color when the field is focused.
44struct RoundedBorderTextFieldStyle : TextFieldStyle {
5- let focused : Bool
5+ private let focused : Bool
6+ private let focusedBorderColor : Color
7+ private let unfocusedBorderColor : Color
8+
9+ init ( focused: Bool ,
10+ focusedBorderColor: Color = Defaults . focusedBorderColor,
11+ unfocusedBorderColor: Color = Defaults . unfocusedBorderColor) {
12+ self . focused = focused
13+ self . focusedBorderColor = focusedBorderColor
14+ self . unfocusedBorderColor = unfocusedBorderColor
15+ }
616
717 func _body( configuration: TextField < Self . _Label > ) -> some View {
818 configuration
919 . padding ( 10 )
1020 . background (
1121 RoundedRectangle ( cornerRadius: 8 , style: . continuous)
12- . stroke ( focused ? Color ( . brand ) : Color . gray ,
22+ . stroke ( focused ? focusedBorderColor : unfocusedBorderColor ,
1323 lineWidth: focused ? 2 : 1 )
1424 )
1525 }
1626}
27+
28+ extension RoundedBorderTextFieldStyle {
29+ enum Defaults {
30+ static let focusedBorderColor : Color = . init( uiColor: . brand)
31+ static let unfocusedBorderColor : Color = . gray
32+ }
33+ }
34+
35+ struct TextFieldStyles_Previews : PreviewProvider {
36+ static var previews : some View {
37+ VStack {
38+ TextField ( " placeholder " , text: . constant( " focused " ) )
39+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true ) )
40+ TextField ( " placeholder " , text: . constant( " unfocused " ) )
41+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: false ) )
42+ TextField ( " placeholder " , text: . constant( " focused with a different color " ) )
43+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true , focusedBorderColor: . orange) )
44+ TextField ( " placeholder " , text: . constant( " unfocused with a different color " ) )
45+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: false , unfocusedBorderColor: . cyan) )
46+ }
47+ . preferredColorScheme ( . dark)
48+ }
49+ }
0 commit comments