Skip to content

Commit d7fe6a7

Browse files
Added Protection Button to enable the Password mode
1 parent ff3df5e commit d7fe6a7

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

Sources/FloatingLabelTextField/FloatingLabelTextField.swift

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import SwiftUI
1616
@available(iOS 15.0, *)
1717
public struct FloatingLabelTextField: View {
1818
public let label: String
19+
public var labelColor: Color
1920
@Binding public var text: String
2021
public var placeholder: String?
2122
public var isSecure: Bool
@@ -25,62 +26,99 @@ public struct FloatingLabelTextField: View {
2526
public var cornerRadius: CGFloat
2627
public var borderColor: Color
2728
public var borderWidth: CGFloat
29+
public var labelFont: Font
30+
public var textFont: Font
31+
/// Local state for secure text visibility
32+
@State private var isPasswordVisible: Bool = false
2833

34+
//MARK: - Init
2935
public init(
3036
label: String,
3137
text: Binding<String>,
3238
placeholder: String? = nil,
3339
isSecure: Bool = false,
40+
labelColor: Color = .black,
3441
keyboardType: UIKeyboardType = .default,
3542
autocapitalization: TextInputAutocapitalization? = .sentences,
36-
height: CGFloat = 40,
37-
cornerRadius: CGFloat = 6,
43+
height: CGFloat = 48,
44+
cornerRadius: CGFloat = 8,
3845
borderColor: Color = .primary,
39-
borderWidth: CGFloat = 1
46+
borderWidth: CGFloat = 1,
47+
labelFont: Font = .system(size: 12),
48+
textFont: Font = .system(size: 16)
4049
){
4150
self.label = label
4251
self._text = text
4352
self.placeholder = placeholder
4453
self.isSecure = isSecure
54+
self.labelColor = labelColor
4555
self.keyboardType = keyboardType
4656
self.autocapitalization = autocapitalization
4757
self.height = height
4858
self.cornerRadius = cornerRadius
4959
self.borderColor = borderColor
5060
self.borderWidth = borderWidth
61+
self.labelFont = labelFont
62+
self.textFont = textFont
5163
}
5264

65+
//MARK: - Body
5366
public var body: some View {
5467
ZStack(alignment: .leading){
5568
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
5669
.stroke(borderColor.opacity(0.9), lineWidth: borderWidth)
70+
.background(
71+
Color(UIColor.systemBackground)
72+
.clipShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
73+
)
74+
75+
Text(label)
76+
.font(labelFont)
77+
.foregroundColor(labelColor)
78+
.padding(.horizontal, 6)
5779
.background(Color(UIColor.systemBackground))
80+
.offset(x: 12, y: -height / 2.1)
81+
.zIndex(1)
5882

59-
VStack(alignment: .leading, spacing: 0){
60-
Text(label)
61-
.font(.system(size: 14))
62-
.foregroundColor(Color.primary)
63-
.padding(.horizontal, 6)
64-
.background(Color(UIColor.systemBackground))
65-
.offset(x: 12, y: -10)
66-
.zIndex(1)
67-
83+
/// Input field with optional eye icon
84+
HStack {
6885
Group {
6986
if isSecure {
70-
SecureField(placeholder ?? "", text: $text)
71-
.textInputAutocapitalization(autocapitalization)
72-
.keyboardType(keyboardType)
87+
if isPasswordVisible {
88+
TextField(placeholder ?? "", text: $text)
89+
.font(textFont)
90+
.textInputAutocapitalization(autocapitalization)
91+
.keyboardType(keyboardType)
92+
} else {
93+
SecureField(placeholder ?? "", text: $text)
94+
.font(textFont)
95+
.textInputAutocapitalization(autocapitalization)
96+
.keyboardType(keyboardType)
97+
}
7398
} else {
7499
TextField(placeholder ?? "", text: $text)
100+
.font(textFont)
75101
.textInputAutocapitalization(autocapitalization)
76102
.keyboardType(keyboardType)
77103
}
78104
}
79-
.padding(.horizontal, 12)
80-
.padding(.vertical, 8)
105+
106+
/// Protection button
107+
if isSecure {
108+
Button(action: {
109+
isPasswordVisible.toggle()
110+
}){
111+
Image(systemName: isPasswordVisible ? "eye.slash.fill" : "eye.fill")
112+
.resizable()
113+
.scaledToFit()
114+
.frame(width: height * 0.45, height: height * 0.45)
115+
.foregroundColor(borderColor)
116+
}
117+
}
81118
}
82-
.padding(.top, 6)
119+
.padding(.horizontal, 15)
120+
.frame(height: height - 16, alignment: .center)
83121
}
84-
.frame(minHeight: height, maxHeight: height)
122+
.frame(height: height)
85123
}
86124
}

0 commit comments

Comments
 (0)