Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created September 27, 2020 10:32
Show Gist options
  • Save prafullakumar/80ff690397a1b049b82266bc0f9833cc to your computer and use it in GitHub Desktop.
Save prafullakumar/80ff690397a1b049b82266bc0f9833cc to your computer and use it in GitHub Desktop.
import SwiftUI
struct FloatingTextField: View {
let textFieldHeight: CGFloat = 50
private let placeHolderText: String
@Binding var text: String
@State private var isEditing = false
public init(placeHolder: String,
text: Binding<String>) {
self._text = text
self.placeHolderText = placeHolder
}
var shouldPlaceHolderMove: Bool {
isEditing || (text.count != 0)
}
var body: some View {
ZStack(alignment: .leading) {
TextField("", text: $text, onEditingChanged: { (edit) in
isEditing = edit
})
.padding()
.overlay(RoundedRectangle(cornerRadius: 8)
.stroke(Color.secondary, lineWidth: 1)
.frame(height: textFieldHeight))
.foregroundColor(Color.primary)
.accentColor(Color.secondary)
.animation(.linear)
///Floating Placeholder
Text(placeHolderText)
.foregroundColor(Color.secondary)
.background(Color(UIColor.systemBackground))
.padding(shouldPlaceHolderMove ?
EdgeInsets(top: 0, leading:15, bottom: textFieldHeight, trailing: 0) :
EdgeInsets(top: 0, leading:15, bottom: 0, trailing: 0))
.scaleEffect(shouldPlaceHolderMove ? 1.0 : 1.2)
.animation(.linear)
}
}
}
struct demo: View {
@State var name: String = ""
@State var email: String = ""
var body: some View {
VStack {
FloatingTextField(placeHolder: "Name", text: $name)
FloatingTextField(placeHolder: "Email", text: $email)
}.padding()
}
}
struct FloatingTextField_Previews: PreviewProvider {
static var previews: some View {
demo()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment