@@ -3,10 +3,17 @@ import SwiftUI
33/// Text field has a rounded border that has a thicker border and brighter border color when the field is focused.
44struct RoundedBorderTextFieldStyle : TextFieldStyle {
55 private let focused : Bool
6+ private let focusedBorderColor : Color
7+ private let unfocusedBorderColor : Color
68 private let insets : EdgeInsets
79
8- init ( focused: Bool , insets: EdgeInsets = Defaults . insets) {
10+ init ( focused: Bool ,
11+ focusedBorderColor: Color = Defaults . focusedBorderColor,
12+ unfocusedBorderColor: Color = Defaults . unfocusedBorderColor,
13+ insets: EdgeInsets = Defaults . insets) {
914 self . focused = focused
15+ self . focusedBorderColor = focusedBorderColor
16+ self . unfocusedBorderColor = unfocusedBorderColor
1017 self . insets = insets
1118 }
1219
@@ -15,14 +22,36 @@ struct RoundedBorderTextFieldStyle: TextFieldStyle {
1522 . padding ( insets)
1623 . background (
1724 RoundedRectangle ( cornerRadius: 8 , style: . continuous)
18- . stroke ( focused ? Color ( . brand ) : Color . gray ,
25+ . stroke ( focused ? focusedBorderColor : unfocusedBorderColor ,
1926 lineWidth: focused ? 2 : 1 )
2027 )
2128 }
2229}
2330
2431extension RoundedBorderTextFieldStyle {
2532 enum Defaults {
33+ static let focusedBorderColor : Color = . init( uiColor: . brand)
34+ static let unfocusedBorderColor : Color = . gray
2635 static let insets = EdgeInsets ( top: 10 , leading: 10 , bottom: 10 , trailing: 10 )
2736 }
2837}
38+
39+ struct TextFieldStyles_Previews : PreviewProvider {
40+ static var previews : some View {
41+ VStack {
42+ TextField ( " placeholder " , text: . constant( " focused " ) )
43+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true ) )
44+ TextField ( " placeholder " , text: . constant( " unfocused " ) )
45+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: false ) )
46+ TextField ( " placeholder " , text: . constant( " focused with a different color " ) )
47+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true , focusedBorderColor: . orange) )
48+ . environment ( \. sizeCategory, . extraExtraExtraLarge)
49+ TextField ( " placeholder " , text: . constant( " unfocused with a different color " ) )
50+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: false , unfocusedBorderColor: . cyan) )
51+ TextField ( " placeholder " , text: . constant( " custom insets " ) )
52+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: false , insets: . init( top: 20 , leading: 0 , bottom: 10 , trailing: 50 ) ) )
53+ . frame ( width: 150 )
54+ }
55+ . preferredColorScheme ( . dark)
56+ }
57+ }
0 commit comments