Skip to content

Instantly share code, notes, and snippets.

@Alhomaidhi
Last active February 1, 2023 16:07
Show Gist options
  • Save Alhomaidhi/a398eb3eccfbdcaca75f524d1e79d573 to your computer and use it in GitHub Desktop.
Save Alhomaidhi/a398eb3eccfbdcaca75f524d1e79d573 to your computer and use it in GitHub Desktop.
Material Design textField to SwiftUI (SwiftUI Use)
//
// MDTextField.swift
// ValidationDemo
//
// Created by Abdullah Alhomaidhi on 15/06/2021.
//
import SwiftUI
struct MDTextField: View {
@Binding var text: String
let iconName: String
let placeholderText: String
let labelText: String
@Binding var errorText: String
let isUndoable: Bool
let isSecureTextEntry: Bool
let onBeginEditing: (()->())?
let onEndEditing: (()->())?
let validateCompletion: (()->())?
init(text: Binding<String>,
iconName: String,
placeholderText: String,
labelText: String,
errorText: Binding<String>,
isUndoable: Bool = false,
isSecureTextEntry: Bool = false,
onBeginEditing: (()->())? = nil,
onEndEditing: (()->())? = nil,
validateCompletion: (()->())? = nil) {
self._text = text
self.iconName = iconName
self.placeholderText = placeholderText
self.labelText = labelText
self._errorText = errorText
self.isUndoable = isUndoable
self.isSecureTextEntry = isSecureTextEntry
self.onBeginEditing = onBeginEditing
self.onEndEditing = onEndEditing
self.validateCompletion = validateCompletion
}
private func CustomTextField() -> some View {
let textfield = UIMDTextField(iconSystemName: iconName, labelText: labelText, placeholderText: placeholderText, text: $text, errorText: $errorText, isSecureTextEntry: isSecureTextEntry, isUndoable: isUndoable, onBeginEditing: onBeginEditing, onEndEditing: onEndEditing, validateCompletion: validateCompletion) { newString in
text = newString
}
textfield.outlinedNormalOutlineColor = .blue
return textfield
}
var body: some View {
CustomTextField()
.padding()
.frame(height: 100)
}
}
struct MDTextField_Previews: PreviewProvider {
static var previews: some View {
VStack {
MDTextField(text: .constant("Phone"), iconName: "key", placeholderText: "********", labelText: "Password", errorText: .constant(""), isUndoable: false, isSecureTextEntry: true, onBeginEditing: {
print("onBeginEditing")
}, onEndEditing: {
print("onEndEditing")
}, validateCompletion: {
print("onEndEditing")
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment