TextField 0.5.3

TextField 0.5.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上一个版本2015年10月
SPM支持 SPM

Elvis Nuñez 维护。



 
依赖关系
Formatter~> 0.3.2
InputValidator~> 0.8.5
Hex~> 2.0
 

TextField 0.5.3

TextField

这是一个支持为有效/无效状态添加样式的 TextField 子类,仅使用布尔值,提供格式化器,可以轻松地对信用卡号、电话号码等进行格式化。它支持输入验证器,可以使 UITextField 的内容限定为最大长度、最大值甚至正则表达式(非常适合验证电子邮件)。

支付示例

演示

代码

lazy var emailTextField: TextField = {
    let textField = TextField(frame: frame)
    textField.inputType = .Email
    textField.placeholder = "Email"

    let validation = Validation()
    validation.required = true
    let inputValidator = InputValidator(validation: validation)
    textField.inputValidator = inputValidator

    return textField
}()

lazy var cardNumberTextField: TextField = {
    let textField = TextField(frame: frame)
    textField.inputType = .Integer
    textField.formatter = CardNumberFormatter()
    textField.placeholder = "Card Number"

    let validation = Validation()
    validation.maximumLength = "1234 5678 1234 5678".characters.count
    validation.required = true
    let inputValidator = NumberInputValidator(validation: validation)
    textField.inputValidator = inputValidator

    return textField
    }()

lazy var cardExpirationDateTextField: TextField = {
    let textField = TextField(frame: frame)
    textField.inputType = .Integer
    textField.formatter = CardExpirationDateFormatter()
    textField.placeholder = "Expiration Date (MM/YY)"

    let validation = Validation()
    validation.maximumLength = "MM/YY".characters.count
    validation.required = true
    let inputValidator = NumberInputValidator(validation: validation)
    textField.inputValidator = inputValidator

    return textField
    }()

lazy var cvcTextField: TextField = {
    let textField = TextField(frame: frame)
    textField.inputType = .Number
    textField.placeholder = "CVC"

    let validation = Validation()
    validation.maximumLength = "CVC".characters.count
    validation.required = true
    let inputValidator = NumberInputValidator(validation: validation)
    textField.inputValidator = inputValidator

    return textField
    }()

样式

TextField 同样支持使用 UIAppearance 协议进行样式设计。上述示例即使用了该方式进行样式设计。

let enabledBackgroundColor = UIColor(hex: "E1F5FF")
let enabledBorderColor = UIColor(hex: "3DAFEB")
let enabledTextColor = UIColor(hex: "455C73")
let activeBorderColor = UIColor(hex: "3DAFEB")

TextField.appearance().borderWidth = 1
TextField.appearance().cornerRadius = 5
TextField.appearance().accessoryButtonColor = activeBorderColor
TextField.appearance().font = UIFont(name: "AvenirNext-Regular", size: 15)

TextField.appearance().enabledBackgroundColor = enabledBackgroundColor
TextField.appearance().enabledBorderColor = enabledBorderColor
TextField.appearance().enabledTextColor = enabledTextColor

TextField.appearance().validBackgroundColor = enabledBackgroundColor
TextField.appearance().validBorderColor = enabledBorderColor
TextField.appearance().validTextColor = enabledTextColor

TextField.appearance().activeBackgroundColor = enabledBackgroundColor
TextField.appearance().activeBorderColor = activeBorderColor
TextField.appearance().activeTextColor = enabledTextColor

TextField.appearance().inactiveBackgroundColor = enabledBackgroundColor
TextField.appearance().inactiveBorderColor = enabledBorderColor
TextField.appearance().inactiveTextColor = enabledTextColor

TextField.appearance().disabledBackgroundColor = UIColor(hex: "F5F5F8")
TextField.appearance().disabledBorderColor = UIColor(hex: "DEDEDE")
TextField.appearance().disabledTextColor = UIColor.whiteColor()

TextField.appearance().invalidBackgroundColor = UIColor(hex: "FFD7D7")
TextField.appearance().invalidBorderColor = UIColor(hex: "EC3031")
TextField.appearance().invalidTextColor = UIColor(hex: "EC3031")

安装

TextField 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'TextField'

许可证

TextField 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。

作者

Elvis Nuñez,@3lvis