How to limit character insertion in TextField

Discussion in 'iOS Programming' started by priyank.ranka, Nov 19, 2008.

  1. priyank.ranka macrumors member

    Jun 11, 2008
    Hi EveryOne,
    I want to limit user from typing characters above certain limit. Suppose i want only ten character long word input from user via TextField and if user tries to input more than ten characters into the TextField the TextField cursor should not move. Any help be appreciated. Thanks for ur help.
  2. Pring macrumors 6502

    Sep 17, 2003
    Look at UITextFieldDelegate shouldChangeCharactersInRange

    Return false if you don't want the characters to be entered.
  3. jnic macrumors 6502a

    Oct 24, 2008
    Something like:

    if (textField.text.length > 10) textField.text = [textField.text substringToIndex:10];
    should do the trick.

    EDIT: Never mind, Pring's solution is much cleaner :)

Share This Page