Format UITextField text without having cursor move to the end

The way to make this work is to grab the location of the cursor, update the field contents, and then replace the cursor to its original position. I’m not sure of the exact equivalent in Swift, but the following is how I would do it in Obj-C. – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { UITextPosition … Read more

Detect backspace Event in UITextField

Swift 4.2 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if let char = string.cString(using: String.Encoding.utf8) { let isBackSpace = strcmp(char, “\\b”) if (isBackSpace == -92) { print(“Backspace was pressed”) } } return true } Older Swift version func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let … Read more

Cannot assign a value of type ViewController to a value of type UITextFieldDelegate?

The line self.MessageTextField.delegate = self causes the error since you try to assign self as the delegate of a UITextField. But your ViewController is not a UITextFieldDelegate. To make your class this kind of delegte, you need to adopt the UITextFieldDelegate protocol. This can be achieved by adding it to the list of protocols and … Read more

Switching between Text fields on pressing return key in Swift

Make sure your UITextField delegates are set and the tags are incremented properly. This can also be done through the Interface Builder. Here’s a link to an Obj-C post I found: How to navigate through textfields (Next / Done Buttons) class ViewController: UIViewController,UITextFieldDelegate { // Link each UITextField (Not necessary if delegate and tag are … Read more

How shouldChangeCharactersInRange works in Swift?

Swift 4, Swift 5 This method doesn’t use NSString // MARK: – UITextFieldDelegate extension MyViewController: UITextFieldDelegate { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if let text = textField.text, let textRange = Range(range, in: text) { let updatedText = text.replacingCharacters(in: textRange, with: string) myvalidator(text: updatedText) } return true } … Read more

UITextField text change event

From proper way to do uitextfield text change call back: I catch the characters sent to a UITextField control something like this: // Add a “textFieldDidChange” notification method to the text field control. In Objective-C: [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; In Swift: textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged) Then in the textFieldDidChange method you can examine the … Read more

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