A powerful, customizable text field component for iOS with built-in validation, formatting, and comprehensive styling support.
- Input Validation: Built-in validator system with support for custom validation rules
- Input Formatting: Real-time text formatting with cursor position management
- Accessory System: Extensible accessory views (leading/trailing) for additional UI elements
- Styling: Comprehensive styling system with state-based theming
- Pure UIKit: Zero external dependencies - built entirely with UIKit
- UIKit Integration: Seamless integration with existing UIKit applications
Current version: 1.0.0
- iOS 13.0+
- Swift 5.9+
- Xcode 14.0+
Add the following dependency to your Package.swift file:
dependencies: [
.package(url: "https://github.com/zhuolingzhao007/ValidatedTextField.git", from: "1.0.0")
]Or add it directly in Xcode:
- Go to File > Add Packages...
- Enter the package URL:
https://github.com/zhuolingzhao007/ValidatedTextField.git - Select the version you want to use
import ValidatedTextField
let textField = ValidatedTextField()
// Set placeholder
textField.placeholder = "Enter your email"
// Configure validation
textField.strategy = ValidationStrategy(
validator: createValidator {
// Email validation logic
Validator { input in
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailRegex)
return emailPredicate.evaluate(with: input) ? .valid : .invalid(message: "Invalid email format")
}
}
)
// Add to your view
view.addSubview(textField)// Observe validation state changes
textField.onValidationChanged = { state in
switch state {
case .valid:
print("Input is valid")
case .invalid(let message):
print("Validation error: \(message)")
case .none:
print("No validation applied")
}
}textField.configureStyle { builder in
builder.backgroundColor(.white)
builder.custom { $0.textField.textColor = .black }
builder.layout { patch in
patch.containerPadding = 16.0
}
builder.onInteraction(.editing) { builder in
builder.custom { $0.layer.borderColor = UIColor.blue.cgColor }
builder.custom { $0.layer.borderWidth = 2.0 }
}
builder.onValidation(.invalid) { builder in
builder.custom { $0.layer.borderColor = UIColor.red.cgColor }
}
}The Style Builder also supports configuring nested objects using configure with NestedBuilder:
textField.configureStyle { builder in
// Configure the main view
builder.backgroundColor(.white)
// Configure nested textField using NestedBuilder
builder.configure(\ValidatedTextField.textField) { nested in
nested.textColor(.black).font(.systemFont(ofSize: 16))
}
}- ValidatedTextField: Main UIView component
- ValidationStrategy: Combines formatting and validation logic
- InputFormatter: Handles text formatting and cursor positioning
- InputValidator: Protocol for validation logic
- AccessoryPlugin: Extensible accessory view system
- Style System: State-based styling system
The validation system supports:
- Custom validation functions
- Builder pattern for combining validators
- Logical operators (AND, OR, NOT)
- Real-time and on-commit validation triggers
- Real-time text formatting
- Cursor position management
- Support for complex formatting rules (phone numbers, credit cards, etc.)
None - Pure UIKit implementation with zero external dependencies.
This project is licensed under the MIT License - see the LICENSE file for details.