Set the maximum character length of a UITextField in Swift

Your view controller should conform to UITextFieldDelegate, like below: class MyViewController: UIViewController, UITextFieldDelegate { } Set the delegate of your textfield: myTextField.delegate = self Implement the method in your view controller: textField(_:shouldChangeCharactersInRange:replacementString:) All together: class MyViewController: UIViewController, UITextFieldDelegate // Set delegate to class @IBOutlet var mytextField: UITextField // textfield variable override func viewDidLoad() { super.viewDidLoad() … Read more

Swift add icon/image in UITextField

Sahil has a great answer and I wanted to take that and expand it into an @IBDesignable so developers can add images to their UITextFields on the Storyboard. Swift 4.2 import UIKit @IBDesignable class DesignableUITextField: UITextField { // Provides left padding for images override func leftViewRect(forBounds bounds: CGRect) -> CGRect { var textRect = super.leftViewRect(forBounds: … Read more

UITextField border color

Import QuartzCore framework in you class: #import <QuartzCore/QuartzCore.h> and for changing the border color use the following code snippet (I’m setting it to redColor), textField.layer.cornerRadius=8.0f; textField.layer.masksToBounds=YES; textField.layer.borderColor=[[UIColor redColor]CGColor]; textField.layer.borderWidth= 1.0f; For reverting back to the original layout just set border color to clear color, serverField.layer.borderColor=[[UIColor clearColor]CGColor]; in swift code textField.layer.borderWidth = 1 textField.layer.borderColor = UIColor.whiteColor().CGColor

Detect backspace in empty UITextField

Swift 4: Subclass UITextField: // MyTextField.swift import UIKit protocol MyTextFieldDelegate: AnyObject { func textFieldDidDelete() } class MyTextField: UITextField { weak var myDelegate: MyTextFieldDelegate? override func deleteBackward() { super.deleteBackward() myDelegate?.textFieldDidDelete() } } Implementation: // ViewController.swift import UIKit class ViewController: UIViewController, MyTextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // initialize textField let input = MyTextField(frame: CGRect(x: 50, … Read more

Max length UITextField

With Swift 5 and iOS 12, try the following implementation of textField(_:shouldChangeCharactersIn:replacementString:) method that is part of the UITextFieldDelegate protocol: func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { guard let textFieldText = textField.text, let rangeOfTextToReplace = Range(range, in: textFieldText) else { return false } let substringToReplace = textFieldText[rangeOfTextToReplace] let count … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)