@@ -6,15 +6,24 @@ struct RoundedBorderTextFieldStyle: TextFieldStyle {
66 private let focusedBorderColor : Color
77 private let unfocusedBorderColor : Color
88 private let insets : EdgeInsets
9+ private let height : CGFloat ?
910
11+ /// - Parameters:
12+ /// - focused: Whether the field is focused or not.
13+ /// - focusedBorderColor: The border color when the field is focused.
14+ /// - unfocusedBorderColor: The border color when the field is not focused.
15+ /// - insets: The insets between the background border and the text input.
16+ /// - height: An optional fixed height for the field.
1017 init ( focused: Bool ,
1118 focusedBorderColor: Color = Defaults . focusedBorderColor,
1219 unfocusedBorderColor: Color = Defaults . unfocusedBorderColor,
13- insets: EdgeInsets = Defaults . insets) {
20+ insets: EdgeInsets = Defaults . insets,
21+ height: CGFloat ? = nil ) {
1422 self . focused = focused
1523 self . focusedBorderColor = focusedBorderColor
1624 self . unfocusedBorderColor = unfocusedBorderColor
1725 self . insets = insets
26+ self . height = height
1827 }
1928
2029 func _body( configuration: TextField < Self . _Label > ) -> some View {
@@ -24,7 +33,9 @@ struct RoundedBorderTextFieldStyle: TextFieldStyle {
2433 RoundedRectangle ( cornerRadius: 8 , style: . continuous)
2534 . stroke ( focused ? focusedBorderColor: unfocusedBorderColor,
2635 lineWidth: focused ? 2 : 1 )
36+ . frame ( height: height)
2737 )
38+ . frame ( height: height)
2839 }
2940}
3041
@@ -51,6 +62,20 @@ struct TextFieldStyles_Previews: PreviewProvider {
5162 TextField ( " placeholder " , text: . constant( " custom insets " ) )
5263 . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: false , insets: . init( top: 20 , leading: 0 , bottom: 10 , trailing: 50 ) ) )
5364 . frame ( width: 150 )
65+ HStack {
66+ TextField ( " placeholder " , text: . constant( " text field " ) )
67+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true ) )
68+ SecureField ( " placeholder " , text: . constant( " secure " ) )
69+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true ) )
70+ }
71+ . environment ( \. sizeCategory, . extraExtraExtraLarge)
72+ HStack {
73+ TextField ( " placeholder " , text: . constant( " text field " ) )
74+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true , height: 100 ) )
75+ SecureField ( " placeholder " , text: . constant( " secure " ) )
76+ . textFieldStyle ( RoundedBorderTextFieldStyle ( focused: true ) )
77+ }
78+ . environment ( \. sizeCategory, . extraExtraExtraLarge)
5479 }
5580 . preferredColorScheme ( . dark)
5681 }
0 commit comments