Set the maximum character length of a UITextField

前端 未结 30 2102
难免孤独
难免孤独 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条回答
  •  滥情空心
    2020-11-22 02:42

    Use this code here RESTRICTED_LENGTH is length you want to restrict for textfield.

       - (BOOL)textField:(UITextField *)textField     shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (textField == nameTF) {
        int limit = RESTRICTED_LENGTH - 1;
        return !([textField.text length]>limit && [string length] > range.length);
        }
       else
       {
        return YES;
       }
    
    return NO;
    
    }
    

提交回复
热议问题