0

I'm new to developing and I am trying to limit the number of characters that can be entered in a textfield, with the most recent version of Swift. I have tried to follow several different tutorials and have checked out a few different answers around the site, but I am not having much luck.

Currently, I have this entered in my swift file:

@IBAction func InfoTextFieldLimit(sender: UITextField) { self.InfoTextField.delegate = self func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let currentCharacterCount = textField.text?.characters.count ?? 0 if (range.length + range.location > currentCharacterCount) { return false } let newLength = currentCharacterCount + string.characters.count - range.length return newLength <= 10 } } 

...I'm not getting any errors, but nothing seems to be happening when I run the app.

I have also tried at least 3 other methods that I didn't bother copying. (but can provide on request)

Can anybody tell me what I'm doing wrong and point me in the right direction? Would be much appreciated! ...I feel like there's a good chance I might be overlooking something that's obvious.

I am also going to want to do this with a textview down the road, how would that be different?

2
  • Here's a link to your question elsewhere: stackoverflow.com/questions/433337/… Commented Mar 30, 2016 at 14:45
  • I think the problem is you did not set the delegate correctly. What is the IBAction method used for? You can set "self" to the textfield delegate in viewDidLoad. Commented Mar 30, 2016 at 14:50

2 Answers 2

1
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let maxLength = 50 guard let text = textField.text else { return true } let newLength = text.characters.count + string.characters.count - range.length return newLength <= maxLength } 

Also make sure you have set the delegate of the UITextField to the UIViewController

Sign up to request clarification or add additional context in comments.

2 Comments

this is useful when you have only one text field But I have 5 text field that I want to limit one of them so what should I do ? please help me
You can check the textField instance itself if (textField == yourTextField) { // do the stuff here } or set a tag to your textFields and check the same in this delegate method. yourTextField.tag = 1000 //set this in code or IB and if (textField.tag == 1000) { // do the stuff here }
0

I originally had this code in objective-c:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSUInteger newLength = [textField.text length] + [string length] - range.length; return (newLength > 4) ? NO : YES; } 

I tried converting it to swift, let me know if it works... You have to use the UITextFieldDelegate method.

func textField( textField:UITextField,shouldChangeCharactersInRange range:NSRange,replacementString string:NSString)->Bool{ var newLength:NSUInteger = textField.text.length + string.length - range.length return null(newLength > 4 )?false:true } 

2 Comments

I actually didn't have luck with this one either, but I was able to get the original code working that I had posted in my question after a little more trial and error. I think I did have my delegate method set wrong.. However now i've encountered another small issue. I have 3 different textfields in my viewcontroller, and I only want to limit the amt. of characters in one of them as opposed to all.. I tried "if (textfield == MyTextField) {" after "Bool {" but now i'm stuck.. How would I accomplish this? ...Thanks for the help!
I'm sorry, I'm not sure I understand what you're saying... The comparison of the two textfields didn't work?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.