Set the maximum character length of a UITextField

While the UITextField class has no max length property, it’s relatively simple to get this functionality by setting the text field’s delegate and implementing the following delegate method: Objective-C – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Prevent crashing undo bug – see note below. if(range.length + range.location > textField.text.length) { return NO; } NSUInteger … Read more

Set padding for UITextField with UITextBorderStyleNone

I found a neat little hack to set the left padding for this exact situation. Basically, you set the leftView property of the UITextField to be an empty view of the size of the padding you want: UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; textField.leftView = paddingView; textField.leftViewMode = UITextFieldViewModeAlways; Worked like a … Read more

Move textfield when keyboard appears swift

There are a couple of improvements to be made on the existing answers. Firstly the UIKeyboardWillChangeFrameNotification is probably the best notification as it handles changes that aren’t just show/hide but changes due to keyboard changes (language, using 3rd party keyboards etc.) and rotations too (but note comment below indicating the keyboard will hide should also … Read more

iOS – Dismiss keyboard when touching outside of UITextField

You’ll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the UITextField on it’s selector. The code: In viewDidLoad UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; In dismissKeyboard: -(void)dismissKeyboard { [aTextField resignFirstResponder]; } (Where aTextField is the textfield that is responsible for the keyboard) … Read more

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

Getting and Setting Cursor Position of UITextField and UITextView in Swift

The following content applies to both UITextField and UITextView. Useful information The very beginning of the text field text: let startPosition: UITextPosition = textField.beginningOfDocument The very end of the text field text: let endPosition: UITextPosition = textField.endOfDocument The currently selected range: let selectedRange: UITextRange? = textField.selectedTextRange Get cursor position if let selectedRange = textField.selectedTextRange { … Read more

How do I check when a UITextField changes?

SWIFT Swift 4.2 textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged) and @objc func textFieldDidChange(_ textField: UITextField) { } SWIFT 3 & swift 4.1 textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged) and func textFieldDidChange(_ textField: UITextField) { } SWIFT 2.2 textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged) and func textFieldDidChange(textField: UITextField) { //your code } OBJECTIVE-C [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; and textFieldDidChange … Read more

How to input currency format on a text field (from right to left) using Swift?

For Swift 3. Input currency format on a text field (from right to left) override func viewDidLoad() { super.viewDidLoad() textField.addTarget(self, action: #selector(myTextFieldDidChange), for: .editingChanged) } @objc func myTextFieldDidChange(_ textField: UITextField) { if let amountString = textField.text?.currencyInputFormatting() { textField.text = amountString } } extension String { // formatting text for currency textField func currencyInputFormatting() -> String … Read more