MB_TextField
is a customizable textField for iOS swift applications. It supports differents types of fields like default, email, password, select and options like floating placeholder, mandatory etc.
MB_TextField
is available through Swift Package Manager.
To add MB_TextField
as a dependency of your Swift package, simply add the following line to your Package.swift
file:
.package(url: "https://github.com/Bejil/MB_TextField", from: "1.0.0")
In order to use MB_TextField
simply add import MB_TextField
at the top of your swift file then use it like a normal UITextField
:
let textField = MB_textField()
textField.placeholder = "Placeholder"
textField.autocapitalizationType = .words
textField.autocorrectionType = .no
textField.returnKeyType = .next
...
MB_TextField
use closures to help you manage events
textField.returnHandler = { textField in
//User has touched the keyboard return key
}
textField.clearHandler = { textField in
//User has touched the clear button
}
textField.changeHandler = { textField in
//User is writting
}
textField.endHandler = { textField in
//User has finished writing (textfield lost focus)
}
textField.beginHandler = { textField in
//User begin writing (textField just got focus)
}
In some cases we want to delay the changeHandler in order to prevent too frequent calls on API.
For that you can change textField.changeDelay
(default = 0.0)
MB_TextField
support multiple types with differents options and text validation:
- None
- Password
- Select
Like native textField
textField.type = .none
With this type, textField use these default values
clearButtonMode = .whileEditing
autocapitalizationType = .sentences
autocorrectionType = .default
keyboardType = .default
returnKeyType = .done
rightViewMode = .always
For email address
textField.type = .email
This type validate the email using this regex : [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}
at the end of editing.
With this type, textField override the .none
type with these values
autocapitalizationType = .none
autocorrectionType = .no
keyboardType = .emailAddress
For password
textField.type = .password
This type validate the password at the end of editing using these criterias:
- Password has between 8 and 40 chars
- Password has at least one lowercased char
- Password has at least one uppercased char
- Password has at least one special char
-_!/@#$%^&*(),.?\":{}
- Password has at least one numeric char
With this type, textField override the .none
type with this value
isSecureTextEntry = true
For using textField like a dropdown button
textField.type = .select
With this type, textField isn't editable and respond to touch using this closure
textField.selectHandler = {
//User just tap the textField
}
MB_TextField
can be customized with these properties:
tintColor
affect the toolbar's buttons and the bordersisValid
affect the bordersisFloatingPlaceholder
affect the placeholder (native or floating)isMandatory
add a mandatory*
to the placeholderisLoading
display aUIActivityIndicatorView
canPaste
affect the paste possibilitybackgroundColor
affect the backgroundtextColor
affect the color of the texttintColor
affect the color of the cursor, the active border and the toolBar buttonsinvalidColor
affect the border color when fieldisValid = false
mandatoryColor
affect the*
color of the placeholder when fieldisMandatory = true
borderColor
affect the default border colorplaceholderColor
affect the placeholder colorisLoading
add aUIActivityIndicatorView
font
the font of the main textplaceholderFont
the font of the placeholdermandatoryFont
the font of the*
in the placeholdermandatory
the text to add to the placeholder ifisMandatory == true
toolbarFont
the font of the toolbar's buttons
You can override MB_TextField
and set all of your custom valeurs in the setUp()
function
public class CustomTextField : MB_TextField {
public override func setUp() {
super.setUp()
placeholderFont = UIFont.boldSystemFont(ofSize: 18)
...
}
}