Class
DSTextField
@IBDesignable public final class DSTextField: UIView, UITextInputTraits
An Enterprise-styled text entry element. It includes properties for providing a placeholder label, helper text that sits below it that educates the user on what text to enter, and DSInputMask and DSValidationRule rules which will determine user input format and display errors when validations failed.
The renderingType decides the appearance of the text field.
The field defaults to single-line, but if isMultiline is set to true the field will grow vertically as text is entered and space is required. When single-line, a UITextField will be used, a UITextView otherwise.
If you need to set field-specific properties such as returnKeyType or autocapitalizationType, use the properties on DSTextField when available. For anything more advanced, the textView and textField properties can be used for direct access.
Use the DSInputMask framework to control text input based on the type of data being entered. There are many built-in rules to use or you can implement your own.
Use the DSValidationRule framework to add one or more validation checks. There are many built-in rules to use or you can implement your own.
For storyboards, add a UIView to your view and change the class name to DSTextField. Also, note that in the storyboard the checkbox for Accessibility is unchecked, however, it will be enabled by default. Ignore the value of this checkbox. If you want to set this value to false, you must do so in code by setting isAccessibilityElement to false.
Since this component can shrink and grow vertically to accommodate its contents, it's recommended that the DSTextField be embedded in a UITableView that makes it easy for its contents to resize smoothly vertically. The easiest way to do this is to use the DSTextFieldDelegate method textFieldDidLayout which gets called when the size changes. Implement it somewhat as follows:
textFieldDidLayout(_ textField: DSTextField) {
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
}
Initialize and set properties as needed.
let bioField = DSTextField(renderingType: .standard)
bioField.labelText = "Bio"
bioField.helperText = "XXX-XXX-XXXX"
bioField.addValidationRule(RequiredValidationRule(message: "Bio is required."))
bioField.clearButtonMode = .whileEditing
Relationships
Conforms To
UITextInputTraits
UIView
Initializers
init(renderingType:)
public init(renderingType: DSTextFieldRenderingType = .cell)
Properties
textFieldDelegate
@IBOutlet public weak var textFieldDelegate: DSTextFieldDelegate?
The delegate which receives updates on the DSTextField's state.
isMultiline
@IBInspectable public var isMultiline: Bool = false
When true, the field will grow vertically as more text is entered. This is similar to standard UITextView behavior except the vertical size is the minimum required to display all text.
isEnabled
@IBInspectable public var isEnabled: Bool = true
Returns true if the component is enabled and allows text editing.
prefixText
@IBInspectable public var prefixText: String?
The text to be displayed in the first position of the DSTextField.
labelText
@IBInspectable public var labelText: String?
The text to be displayed in the DSTextField's label.
helperText
@IBInspectable public var helperText: String?
The text to be displayed as helper to the user on what to enter into the field.
isSecureText
@IBInspectable public var isSecureText: Bool = false
The text is sensitive and you should be protected from viewing. Replaces characters with dots.
showsShowHideButtonWithSecureText
@IBInspectable public var showsShowHideButtonWithSecureText: Bool = true
When isSecureText is true, this property determines if a show/hide button should display. Doesn't have any effect unless isSecureText is true.
validationState
public var validationState: DSValueValidationState
The current validation state based on validation rules you have.
appliesBackgroundColorToSuperview
@IBInspectable public var appliesBackgroundColorToSuperview: Bool = false
Automatically makes superview background color match when enabled/disabled
inputMask
public var inputMask: DSInputMask?
An input mask that formats entered text and/or prevents certain characters from being entered at all
rawText
public var rawText: String
Returns the raw text entered into the DSTextField if a DSInputMask is defined, otherwise returns the text as displayed.
unprefixedText
public var unprefixedText: String
Returns the text as it appears in DSTextField minus prefixing.
validationRules
public var validationRules
The array of ValidationRule objects to be used for validating the current state of the DSTextField contents.
isValid
public var isValid: Bool
Returns true if there are no validations or any validations that exist pass.
returnKeyType
public var returnKeyType: UIReturnKeyType = .default
Specifies the type of return button type of the associated keyboard for the field
keyboardType
public var keyboardType: UIKeyboardType = .default
Specifies the type of keyboard for the field
textContentType
public var textContentType: UITextContentType! = nil
Specifies the semantic meaning for the field
autocorrectionType
public var autocorrectionType: UITextAutocorrectionType = .default
Specifies if words should be autocorrected as typed
spellCheckingType
public var spellCheckingType: UITextSpellCheckingType = .default
Specifies if words should be spell checked as typed
autocapitalizationType
public var autocapitalizationType: UITextAutocapitalizationType = .sentences
Specifies how capitalization happens as typed
accessibilityLabel
public override var accessibilityLabel: String?
Specifies the accessibilityLabel for the entire field
textView
public var textView: UITextView!
The embedded UITextView in case additional customization is needed. (Note that some properties might be overridden by DSTextField, so try to avoid this if at all possible.)
textField
public var textField: UITextField!
The embedded UITextField in case additional customization is needed. (Note that some properties might be overridden by DSTextField, so try to avoid this if at all possible.)
renderingType
public var renderingType: DSTextFieldRenderingType = .cell
The visual style of the field. Can be changed after initialization.
renderingTypeString
@IBInspectable public var renderingTypeString: String
The renderingType property representable by a String.
This property is only recommended for use in the Attributes inspector of a storyboard since enums are not @IBInspectable. Possible values are standard and freeform. In code, use the renderingType property instead.
clearButtonMode
public var clearButtonMode: UITextField.ViewMode = .whileEditing
Determined if, and when, the clear button displays on the component that allows the input text to be reset.
clearButtonModeString
@IBInspectable public var clearButtonModeString: String
The clear button mode representable by a String.
This property is only recommended for use in the Attributes inspector of a storyboard since enums are not @IBInspectable. Possible values are "always", "never", "whileEditing" and "unlessEditing". In code, use the clearButtonMode property instead.
intrinsicContentSize
public override var intrinsicContentSize: CGSize
Methods
awakeFromNib()
public override func awakeFromNib()
prepareForInterfaceBuilder()
public override func prepareForInterfaceBuilder()
layoutSubviews()
public override func layoutSubviews()
becomeFirstResponder()
@discardableResult public override func becomeFirstResponder() -> Bool
resignFirstResponder()
@discardableResult public override func resignFirstResponder() -> Bool
add(validationRule:)
func add(validationRule rule: DSValidationRule)
Adds a validation rule to be used for validating the current value.
Parameters
Name | Type | Description |
---|---|---|
validationRule | DSValidationRule |
A ValidationRule object to be used for checking a validation |
add(validationRules:)
func add(validationRules rules: [DSValidationRule])
Adds an array of validation rules to be used for validating the current value.
Parameters
Name | Type | Description |
---|---|---|
validationRules | [DSValidationRule] |
A ValidationRule array to be used for checking validations |
validate()
func validate()
Explicitly request validation to occur. (This is done automatically by default.)
attachInputAccessoryView(done:)
func attachInputAccessoryView(done: Bool = true)
Attaches a standard accessory view for this field.
Parameters
Name | Type | Description |
---|---|---|
done | Bool |
Adds a "Done" button if true |