user832
user832

Reputation: 836

Which delegate method is called when i manually assign text to a UITextField in iOS Swift 3

let me explain my question with an example

I have a UITextField and want to limit its text and also have a UILabel that tells how many more characters can the user type. Thus when I start typing, everything is running great, I am using this delegate function:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}

But when i assign text like this : textField.text = "Hello", the above mention method is not called and thus the counter label value is not changed,

I have also tried: textField?.addTarget(self, action: #selector(txtFieldChanged), for: .allEditingEvents)

but no luck

Upvotes: 0

Views: 1561

Answers (1)

Raman Srivastava
Raman Srivastava

Reputation: 396

According to you question when you are assigning text to your textfield like "hello" then you have to calculate the textfield length in your view did load method and set the counter there. And when you will start typing in you text field your delegate will be called.

double left = 500 - textView.text.length ;

lblcounter.text = [NSString stringWithFormat:@"%.0f character left",left];

Upvotes: 1

Related Questions