1- // The Swift Programming Language
2- // https://docs.swift.org/swift-book
3-
1+ //
2+ // FloatingLabelTextField.swift
3+ // FloatingLabelTextField
4+ //
5+ // Created by Usama Javed on 12/10/2025.
6+ //
7+ // A customizable SwiftUI text field with an outlined border and floating label.
8+ // Supports secure input, keyboard customization, and flexible UI configuration.
9+ //
10+ // License: MIT License
11+ // Copyright © 2025 Usama Javed. All rights reserved.
12+ //
413
514import SwiftUI
615
@@ -12,27 +21,39 @@ public struct FloatingLabelTextField: View {
1221 public var isSecure : Bool
1322 public var keyboardType : UIKeyboardType
1423 public var autocapitalization : TextInputAutocapitalization ?
24+ public var height : CGFloat
25+ public var cornerRadius : CGFloat
26+ public var borderColor : Color
27+ public var borderWidth : CGFloat
1528
1629 public init (
1730 label: String ,
1831 text: Binding < String > ,
1932 placeholder: String ? = nil ,
2033 isSecure: Bool = false ,
2134 keyboardType: UIKeyboardType = . default,
22- autocapitalization: TextInputAutocapitalization ? = . sentences
35+ autocapitalization: TextInputAutocapitalization ? = . sentences,
36+ height: CGFloat = 40 ,
37+ cornerRadius: CGFloat = 6 ,
38+ borderColor: Color = . primary,
39+ borderWidth: CGFloat = 1
2340 ) {
2441 self . label = label
2542 self . _text = text
2643 self . placeholder = placeholder
2744 self . isSecure = isSecure
2845 self . keyboardType = keyboardType
2946 self . autocapitalization = autocapitalization
47+ self . height = height
48+ self . cornerRadius = cornerRadius
49+ self . borderColor = borderColor
50+ self . borderWidth = borderWidth
3051 }
3152
3253 public var body : some View {
3354 ZStack ( alignment: . leading) {
34- RoundedRectangle ( cornerRadius: 6 , style: . continuous)
35- . stroke ( Color . primary . opacity ( 0.9 ) , lineWidth: 1 )
55+ RoundedRectangle ( cornerRadius: cornerRadius , style: . continuous)
56+ . stroke ( borderColor . opacity ( 0.9 ) , lineWidth: borderWidth )
3657 . background ( Color ( UIColor . systemBackground) )
3758
3859 VStack ( alignment: . leading, spacing: 0 ) {
@@ -56,10 +77,10 @@ public struct FloatingLabelTextField: View {
5677 }
5778 }
5879 . padding ( . horizontal, 12 )
59- . padding ( . vertical, 10 )
80+ . padding ( . vertical, 8 )
6081 }
6182 . padding ( . top, 6 )
6283 }
63- . frame ( minHeight: 48 )
84+ . frame ( minHeight: height , maxHeight : height )
6485 }
6586}
0 commit comments