Skip to content

abspr/TFManager

Repository files navigation

TFManager

Let's say you have multiple UITextFields to get data from users. You need to handle each field keyboard's return key and add an accessory view to the keyboard for navigating through fields. TFManager will do this for you in just one line of code! And if you want more you can add validation rules to the text fields and check if they're valid or not.

iOS SPM MIT

Navigate

Installation

Ready for use on iOS and iPadOS 11+.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/abspr/TFManager", .upToNextMajor(from: "1.1.0"))
]

CocoaPods:

CocoaPods is a dependency manager. For usage and installation instructions, visit their website. To integrate using CocoaPods, specify it in your Podfile:

pod 'TFManager'

Manually

If you prefer not to use any of dependency managers, you can integrate manually. Put Sources/TFManager folder in your Xcode project. Make sure to enable Copy items if needed and Create groups.

Basic Usage

  1. Create an instance of TFManager in your viewController
var fieldsManager = TFManager()
  1. Add your textFields to it:
fieldsManager.add([nameField, mailField, ageField])
  1. There is no more steps 😯

Validate All Fields

You can add rules to your UITextFields and ask TFManager to apply validation to all of child fields:

  1. Change your textField class to ValidatableField.

  1. Then you can call validate() method on your TFManager instance.
let result = fieldsManager.validate()
result.forEach { (invalidField, validationResult) in
    invalidField.textColor = .systemRed
    print(validationResult.message)
}

💡 You can set TFManager's delegate and use its methods to get notified which textField is become active or its text is changing:

fieldsManager.delegate = self

extension ViewController: TFManagerDelegate {
    func textDidChange(_ textField: UITextField, validationResult: ValidationResult?) {
        guard let validationResult = validationResult else { return }
        textField.textColor = validationResult.isValid ? .label : .systemRed
    }
}

💡 You also can subclass ValidatableField and customize your textField. Override didFailValidation(_:) and didPass() methods to handle valid/invalid states (eg: show/hide the error label)

Rules

TFManager comes with set of rules (TextRulesSet) and you can add them to any ValidatableField:

ageField.rulesRepo.add(TextRulesSet.numbersOnly())
ageField.rulesRepo.add(TextRulesSet.minLenght(1))
ageField.rulesRepo.add(TextRulesSet.maxLenght(2))

💡 You can have your own rules too. Just create a struct and implement TextRule:

struct YourRule: TextRule {
    var message: String

    func validate(_ text: String) -> Bool {
        // code
    }
}

Contact

email : hosein@me.com

License

TFManager is available under the MIT license. See the LICENSE file for more info.

About

Add validations to your text fields, Group them together and navigate through them via keyboard's return button and accessory view.

Resources

License

Stars

Watchers

Forks

Packages

No packages published