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?

SASM
  • 1,292
  • 1
  • 22
  • 44
aspenfox
  • 1
  • 1
  • 1
  • Here's a link to your question elsewhere: http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield?rq=1 – Neil Shweky Mar 30 '16 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. – Surely Mar 30 '16 at 14:50

2 Answers2

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

Mathews
  • 733
  • 5
  • 11
  • 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 – Saeed Rahmatolahi Jul 19 '17 at 09:37
  • 2
    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 }` – Mathews Mar 20 '18 at 08:56
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

}
Neil Shweky
  • 893
  • 2
  • 10
  • 23
  • 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! – aspenfox Mar 31 '16 at 21:11
  • I'm sorry, I'm not sure I understand what you're saying... The comparison of the two textfields didn't work? – Neil Shweky Apr 01 '16 at 02:42