ReusableAuthentication is a powerful, pure-Swift library for Reuse Your Login and Signup Page with UI and Validations. It provides you a chance to use a pure-Swift way to work with remote ReusableAuthentication in your next app.
Everything has a beginning. For using a framework, it's installation.
ReusableAuthentication supports from Swift 4 from version 5.0. If you are trying to use ReusableAuthentication with an earlier Swift version, see the release page to find what you need. We will use the latest version in this guide. But the installation process should be the same for all versions except the version number.
From Xcode 11, you can use Swift Package Manager to add ReusableAuthentication to your project.
-
Select File > Swift Packages > Add Package Dependency. Enter https://github.com/YogeshPateliOS/ReusableAuthentication.git in the "Choose Package Repository" dialog.
-
In the next page, specify the version resolving rule as "Branch" with "master".
- After Xcode checking out the source and resolving the branch, you can choose the "ReusableAuthentication" library and add it to your app target.
If you encounter any problem or have a question on adding package to an Xcode project, I suggest the Adding Package Dependencies to Your App guide article from Apple.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate ReusableAuthentication into your Xcode project using CocoaPods, specify it to a target in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target 'MyApp' do
# your other pod
# ...
pod 'ReusableAuthentication', '~> 0.8'
end
Then, run the following command:
$ pod install
You should open the {Project}.xcworkspace instead of the {Project}.xcodeproj after you installed anything from CocoaPods. For more information about how to use CocoaPods, I suggest this tutorial.
- Reuse UITextfield.
- Change Color Properties of Textfield: Font-Color, Background-Color, Text-Color.
- Add Customizable Placeholder text, Add and Change Position of Textfield Image.
- Validations: Email, Password, Phonenumber, Character Length Also you can add your regex or validation easily.
- Easily add DatePicker birthdate like textfield.
- View extensions for
UIString
,UITextfield
andUIAlertController
to directly add validations and show alert.
The simplest use-case is Drag UIView to your UIViewController in Interface and click on identity inspector add Custom Class ReusableLogin
Yeah! Now just bulid and run your project you find 6 Textfields there. right ? Yeah! Next step...
First Step Import ReusableAuthentication
on your swift file where you have to use this.. Like ViewController.swift
import ReusableAuthentication
Create IBOutlet for ReusableLogin
UIView.
@IBOutlet weak var reusableView: ReusableLogin!
Now i want 3 textfields then use this:
@IBOutlet weak var viewHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
reusableView.numberOfTextfields = 3
reusableView.textFieldHeight = 50
reusableView.spacingForTextfields = 8
viewHeight.constant = reusableView.stackView.bounds.height // Height Constrain of your reusableView, Give this height to stackview height so it looks proper.
}
It gives you array UITextfields:
reusableView.textFields
If you want first textfields:
reusableView.textFields.first
lazy var txtEmail: CustomTextfield = {
return reusableView.textFields[0] OR reusableView.textFields.first!
}()
For accessing first textfield Email
no need to write reusableView.textFields[0] just write txtEmail!!
Let's Add Placeholder text and color for our Textfield:
reusableView.textfieldsPlaceholderAndColors(placeholders: ["Email", "Password", "Phone number"], colors: [.red, .blue, .black])
Same color on all textfield placeholders:
reusableView.textfieldsPlaceholderWithColor(placeholders: ["Email", "Password", "Phone number"], color: .black)
KeyboardTypes For All Textfields:
reusableView.textfieldsKeyboardType(keyboardTypes: [.default, .namePhonePad, .numbersAndPunctuation])
Hide Keyboard by touching anywhere:
self.hideKeyboardWhenTappedAround()
Add Image in Textfield like adding key image in Password
reusableView.textFields[1].textfieldLeftImage = UIImage(named: "Your Image Name")
reusableView.textFields[1].leftPadding = 8
reusableView.textFields[1].rightPadding = 8
same check for
fontName, fontSize,fontColor,customTextAlignment,borderColor,letterSpacing,cornerRadius ,customPlaceholer,horizontalInset,verticalInset,borderWidth,baseLineOffset
reusableView.textFields[1].fontSize = 18
textfield. // fine datepicker method like below
reusableView.textFields[2].openDatePicker() // Default Data style - Medium
reusableView.textFields[2].openDatePicker(dateFormateStyle: .full)
Let's check all textfields are empty or not
let isAllEmpty = reusableView.textfieldsIsEmpty()
print(isAllEmpty) // It gives you true or false...
Check Length for textfield
let charLength = reusableView.textFields.first?.text?.validateLength() // default length is mini = 8 and max = 18
//charlength return Bool
add Your custom length for textfield
let _ = reusableView.textFields.first?.text?.validateLength(mini: 5, max: 13)
add Validation for all textfields and also you can add your custom text message.. if it not validate then it showing your message with alert.. if it validate then it gives success in your completion. :-)
reusableView.validations(vc: self, validations: [.email, .password, .phonenumber], validationMessage: ["Email is wrong", "Password is incorrect", "Please check your phonenumber once"]) { (str) in
print(str) // success
}
Seperate Validation for textfields
For Email:
reusableView.textFields.first?.text?.validateEmailId()
Password:
reusableView.textFields.first?.text?.validatePassword()
And Phonenumber
reusableView.textFields.first?.text?.validatePhoneNumber()
please check more validations...on String
extension.
add Your Alert and Actionsheet easily with custom messages:
self.openAlert(title: "Alert", message: "Please Validate All Textfields", alertStyle: .alert, actionTitles: ["Okay", "Cancel"], actionStyles: [.default, .cancel], actions: [{_ in }, {_ in }])
Follow and contact me on Twitter or Youtube. If you find an issue, just open a ticket. Pull requests are warmly welcome as well.
- iOS 12.0+
- Swift 4.0+
ReusableAuthentication is released under the MIT license. See LICENSE for details.