Set the maximum character length of a UITextField

前端 未结 30 2389
难免孤独
难免孤独 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:57

    To complete August answer, an possible implementation of the proposed function (see UITextField's delegate).

    I did not test domness code, but mine do not get stuck if the user reached the limit, and it is compatible with a new string that comes replace a smaller or equal one.

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        //limit the size :
        int limit = 20;
        return !([textField.text length]>limit && [string length] > range.length);
    }
    

提交回复
热议问题