Set the maximum character length of a UITextField

前端 未结 30 1649
难免孤独
难免孤独 2020-11-22 02:27

How can I set the maximum amount of characters in a UITextField on the iPhone SDK when I load up a UIView?

相关标签:
30条回答
  • Often you have multiple input fields with a different length.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        int allowedLength;
        switch(textField.tag) {
            case 1: 
                allowedLength = MAXLENGTHNAME;      // triggered for input fields with tag = 1
                break;
            case 2:
                allowedLength = MAXLENGTHADDRESS;   // triggered for input fields with tag = 2
                break;
            default:
                allowedLength = MAXLENGTHDEFAULT;   // length default when no tag (=0) value =255
                break;
        }
    
        if (textField.text.length >= allowedLength && range.length == 0) {
            return NO; // Change not allowed
        } else {
            return YES; // Change allowed
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:45

    swift 3.0

    This code is working fine when you are paste string more than your character limits.

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            let str = (textView.text + text)
            if str.characters.count <= 10 {
                return true
            }
            textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10))
            return false
        }
    

    Thanks for your votes. :)

    0 讨论(0)
  • 2020-11-22 02:45

    I did this in Swift for an 8 character limit when using a number pad.

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        return !(textField.text?.characters.count == MAX_LENGTH && string != "")
    }
    

    I had to test for string != "" to allow the delete button to work on the number pad, otherwise it wouldn't allow deleting characters in the text field after it reached its max.

    0 讨论(0)
  • 2020-11-22 02:48

    Thank you august! (Post)

    This is the code that I ended up with which works:

    #define MAX_LENGTH 20
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (textField.text.length >= MAX_LENGTH && range.length == 0)
        {
            return NO; // return NO to not change text
        }
        else
        {return YES;}
    }
    
    0 讨论(0)
  • 2020-11-22 02:49

    This should be enough to solve the problem (replace 4 by the limit u want). Just make sure to add delegate in IB.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
         NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
         return (newString.length<=4);
    }
    
    0 讨论(0)
  • 2020-11-22 02:49

    Got it down to 1 line of code :)

    Set your text view's delegate to "self" then add the <UITextViewDelegate> in your .h and the following code in your .m .... you can adjust the number "7" to be whatever you want your MAXIMUM number of characters to be.

    -(BOOL)textView:(UITextView *)a shouldChangeTextInRange:(NSRange)b replacementText:(NSString *)c {
        return ((a.text.length+c.length<=7)+(c.length<1)+(b.length>=c.length)>0);
    }
    

    This code accounts for typing new characters, deleting characters, selecting characters then typing or deleting, selecting characters and cutting, pasting in general, and selecting characters and pasting.

    Done!






    Alternatively, another cool way to write this code with bit-operations would be

    -(BOOL)textView:(UITextView *)a shouldChangeTextInRange:(NSRange)b replacementText:(NSString *)c {
        return 0^((a.text.length+c.length<=7)+(c.length<1)+(b.length>=c.length));
    }
    
    0 讨论(0)
提交回复
热议问题