Last active
January 22, 2019 14:12
-
-
Save shishirthedev/f81d7b5e9c18b2f96c5c54623ba5d320 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public protocol SSTextFieldDelegate { | |
public func texfieldShouldBeginEditing (textField: UITextField) -> Bool | |
public func textFieldShouldEndEditing (textField: UITextField) -> Bool | |
public func textFieldShouldReturn (textField: UITextField) -> Bool | |
public func textFieldDidChanged (textField: UITextField) | |
} | |
// Default implementation of SSTextFieldDelegate | |
extension SSTextFieldDelegate{ | |
func texfieldShouldBeginEditing (textField: UITextField) -> Bool { return true } | |
func textFieldShouldEndEditing (textField: UITextField) -> Bool { return true } | |
func textFieldShouldReturn (textField: UITextField) -> Bool { return true } | |
func textFieldDidChanged (textField: UITextField) { } | |
} | |
public class SSTextField: UITextField, UITextFieldDelegate { | |
@IBInspectable var charsLimit: Int = Int.max // Initially setting limit as max | |
@IBInspectable var bannedChars: String = "" // Initially no banned characters | |
public var ssDelegate: SSTextFieldDelegate? | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
// Config of Textfield | |
delegate = self | |
autocorrectionType = .no | |
spellCheckingType = .no | |
clearButtonMode = .whileEditing | |
// Adding Target to for text change | |
addTarget(self, action: #selector(SSTextField.textFieldDidChange(_:)), for: .editingChanged) | |
} | |
func textField(_ textField: UITextField, | |
shouldChangeCharactersIn range: NSRange, | |
replacementString string: String) -> Bool { | |
let currentString: NSString = textField.text! as NSString | |
let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString | |
return newString.length <= charsLimit && | |
bannedChars.range(of: string, options: .caseInsensitive) == nil | |
} | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
guard let delegate = ssDelegate else { return true } | |
return (delegate.textFieldShouldReturn(textField: textField)) | |
} | |
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { | |
guard let delegate = ssDelegate else { return true } | |
return (delegate.texfieldShouldBeginEditing(textField: textField)) | |
} | |
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { | |
guard let delegate = ssDelegate else { return true } | |
return (delegate.textFieldShouldEndEditing(textField: textField)) | |
} | |
@objc func textFieldDidChange(_ textField: UITextField){ | |
guard let delegate = ssDelegate else { return } | |
delegate.textFieldDidChanged(textField: textField) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment