Skip to content

Instantly share code, notes, and snippets.

@kishikawakatsumi
Created July 24, 2013 18:03
Show Gist options
  • Save kishikawakatsumi/6072953 to your computer and use it in GitHub Desktop.
Save kishikawakatsumi/6072953 to your computer and use it in GitHub Desktop.
Set Max Character Length UITextField (works with Japanese input method)
...
[textField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
...
#define MAXLENGTH 3
- (IBAction)editingChanged:(id)sender
{
UITextField *textField = sender;
UITextRange *textRange = textField.markedTextRange;
if (!textRange.start || !textRange.end) {
if (textField.text.length > MAXLENGTH) {
textField.text = [textField.text substringToIndex:MAXLENGTH];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment