Set the maximum character length of a UITextField

前端 未结 30 1701
难免孤独
难免孤独 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: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);
    }
    

提交回复
热议问题