Skip to content

Instantly share code, notes, and snippets.

@0x7466
Last active September 9, 2015 13:58
Show Gist options
  • Save 0x7466/a36644705d7ae1dd8600 to your computer and use it in GitHub Desktop.
Save 0x7466/a36644705d7ae1dd8600 to your computer and use it in GitHub Desktop.
//
// TextFieldValidationError.swift
// TFTextField
//
// Created by Tobias Feistmantl on 07/09/15.
// Copyright (c) 2015 Tobias Feistmantl. All rights reserved.
//
import Foundation
enum TextFieldValidationError: String {
case Empty = "The field is empty"
case TooShort = "The value is too short"
case TooLong = "The value is too long"
case InvalidEmailAddress = "This isn't a valid email address"
}
//
// TFTextField.swift
// TFTextField
//
// Created by Tobias Feistmantl on 07/09/15.
// Copyright (c) 2015 Tobias Feistmantl. All rights reserved.
//
import Foundation
@IBDesignable class TFTextField: UITextField {
// To be valid...
// ...the text field can't be blank
@IBInspectable var mustBePresent: Bool = false
// ...the value must have a minimum length
@IBInspectable var minimumLength: Int = 0
// ...the value could not have a bigger value
@IBInspectable var maximumLength: Int = 0
// ...the value must be a valid email address
@IBInspectable var isEmailAddress: Bool = false
// The error info label
// Will be set only if an error is set
// and set to nil if the error will be removed
var errorLabel: UILabel?
// Returns true if the given email string has a valid format
func isValidEmail(testString: String) -> Bool {
return NSPredicate(format:"SELF MATCHES %@", "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}").evaluateWithObject(testString)
}
// Validates the value
// Based on the configured settings the value will be valid or not
// Returns a TextFieldValidationError if the value is invalid or nil if not
var validationError: TextFieldValidationError? {
if mustBePresent && empty {
return .Empty
}
if minimumLength != 0 && text.length < minimumLength && (mustBePresent || filled) {
return .TooShort
}
if maximumLength != 0 && text.length > maximumLength {
return .TooLong
}
if isEmailAddress && !isValidEmail(text) && (mustBePresent || filled) {
return .InvalidEmailAddress
}
}
// Returns true if the value is valid
var valid: Bool {
return validationError == nil
}
// Returns true if the value is invalid
var invalid: Bool {
return validationError != nil
}
// Set validation error label in the left side of the text field
// Should be only used with wide text fields!
func setInlineErrorMessage(value: String) {
if errorLabel == nil {
let errorColor = UIColor(hex: "AA0000")
self.textColor = errorColor
self.errorLabel = UILabel()
errorLabel!.textColor = errorColor
errorLabel!.font = self.font
errorLabel!.textAlignment = .Right
errorLabel!.setTranslatesAutoresizingMaskIntoConstraints(false)
errorLabel!.text = value
self.addSubview(errorLabel!)
let centerConstraint = NSLayoutConstraint(item: self, attribute: .CenterY, relatedBy: .Equal, toItem: errorLabel, attribute: .CenterY, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: self, attribute: .Trailing, relatedBy: .Equal, toItem: errorLabel, attribute: .Trailing, multiplier: 1, constant: 0)
self.addConstraints([centerConstraint, rightConstraint])
}
}
// Set error info based on the validation error
func setInlineErrorMessageByValidationError() {
if let error = validationError {
setInlineErrorMessage(error.rawValue)
}
}
// Reset error information
func resetError() {
self.textColor = .blackColor()
errorLabel?.removeFromSuperview()
errorLabel = nil
}
// Compares it's own value with another textfields value
// and return true if the values are equal
func hasEqualValue(textField: UITextField) -> Bool {
return text == textField.text
}
// Returns true if the text field is empty
var empty: Bool {
return text == ""
}
// Returns true if a value is present
var filled: Bool {
return text != ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment