ReusableAuthentication
ReusableAuthentication 是一个强大的纯 Swift 库,可用于重用登录和注册页面(带有 UI 和验证)。它为您提供了使用纯 Swift 方式在下一个应用程序中独立与远程 ReusableAuthentication 交互的机会。
安装指南
一切皆有其始,而对于使用框架,始于安装。
版本
ReusableAuthentication 从 5.0 版本开始支持 Swift 4。如果您尝试使用较旧版本的 Swift 使用 ReusableAuthentication,请参阅发布页面以找到您所需的内容。本指南将使用最新版本,但安装过程除版本号外对所有版本都应相同。
安装
Swift 包管理器
注意事项
从 Xcode 11 开始,您可以使用 Swift 包管理器 将 ReusableAuthentication 添加到项目中。
-
选择“文件”>“Swift 包”>“添加包依赖项”。在“选择包仓库”对话框中输入 https://github.com/YogeshPateliOS/ReusableAuthentication.git。
-
在下一页中,将版本解析规则指定为“分支”,并选择“master”。
- 当 Xcode 检出源并解析分支后,您可以选择“ReusableAuthentication”库并将其添加到您的应用目标中。
如果遇到任何问题或对将包添加到 Xcode 项目有疑问,我建议参考 Apple 的 将包依赖项添加到您的应用 指南文章。
CocoaPods
CocoaPods 是 Cocoa 项目的依赖项管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 ReusableAuthentication 集成到 Xcode 项目中,在 Podfile 中指定它到目标
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target 'MyApp' do
# your other pod
# ...
pod 'ReusableAuthentication', '~> 0.7'
end
然后,运行以下命令
$ pod install
在安装任何来自 CocoaPods 的内容后,您应该打开 {Project}.xcworkspace 而不是 {Project}.xcodeproj。有关如何使用 CocoaPods 的更多信息,我建议参考 此教程。
功能
- 重用 UITextfield。
- 更改 Textfield 的颜色属性:字体颜色、背景颜色、文本颜色。
- 添加可定制的占位符文本,左图和右图及其填充。
- 验证:电子邮件、密码、电话号码、字符长度。您还可以轻松添加您的正则表达式或验证。
- 为
UIString
、UITextfield
和UIAlertController
提供视图扩展,可以直接添加验证和显示警报。
可重复使用的身份验证
最简单的用例是将 UIView 拖放到 Interface 中的 UIViewController 中,然后点击身份检查器,添加自定义类 ReusableLogin
是的!现在只是构建并运行您的项目,你会在那里找到 6 个 Textfields。对吧?下一步...
第一步:在您必须使用此功能的 swift 文件中导入 ReusableAuthentication
,例如 ViewController.swift
import ReusableAuthentication
为 ReusableLogin
UIView 创建 IBOutlet。
@IBOutlet weak var reusableView: ReusableLogin!
现在我想有 3 个 textfields,就用这个
reusableView.numberOfTextfields = 3
它会给您一个 UITextfields 数组
reusableView.textFields
如果您想要第一个 textfields
reusableView.textFields.first
让我们给我们的 Textfield 添加占位文本和颜色
reusableView.textfieldsPlaceholderAndColors(placeholders: ["Email", "Password", "Phone number"], colors: [.red, .blue, .black])
所有 textfield 占位符上的颜色都相同
reusableView.textfieldsPlaceholderWithColor(placeholders: ["Email", "Password", "Phone number"], color: .black)
在 Textfield 中添加图像,就像在密码中添加键图像一样
reusableView.textFields[1].textfieldLeftImage = UIImage(named: "Your Image Name")
reusableView.textFields[1].leftPadding = 8
reusableView.textFields[1].rightPadding = 8
相同的检查
fontName, fontSize,fontColor,customTextAlignment,borderColor,letterSpacing,cornerRadius ,customPlaceholer,horizontalInset,verticalInset,borderWidth,baseLineOffset
reusableView.textFields[1].fontSize = 18
验证
让我们检查所有文本字段是否为空
let isAllEmpty = reusableView.textfieldsIsEmpty()
print(isAllEmpty) // It gives you true or false...
检查文本字段的长度
let charLength = reusableView.textFields.first?.text?.validateLength() // default length is mini = 8 and max = 18
//charlength return Bool
为文本字段添加自定义长度
let _ = reusableView.textFields.first?.text?.validateLength(mini: 5, max: 13)
为所有文本字段添加验证,也可以添加自定义文本消息。如果不验证,则在警报中显示您的消息。如果验证成功,则完成时给出成功。
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
}
为文本字段单独验证
For Email:
reusableView.textFields.first?.text?.validateEmailId()
Password:
reusableView.textFields.first?.text?.validatePassword()
And Phonenumber
reusableView.textFields.first?.text?.validatePhoneNumber()
请检查 String
扩展中更多的验证...
轻松使用自定义消息添加警报和操作表
self.openAlert(title: "Alert", message: "Please Validate All Textfields", alertStyle: .alert, actionTitles: ["Okay", "Cancel"], actionStyles: [.default, .cancel], actions: [{_ in }, {_ in }])
联系
在 Twitter 或 Youtube 上关注并联系我。如果您发现任何问题,请提交工单。非常欢迎 Pull requests。
要求
- iOS 12.0+
- Swift 4.0+
许可证
ReusableAuthentication遵从MIT许可证发布。有关详细信息,请参阅LICENSE文件。