UITextField输入长度限制

[_yourTextField addTarget:self action:@selector(eventEditingChange:) forControlEvents:UIControlEventEditingChanged];

-(void)eventEditingChange:(UITextField *)sender {

    UITextRange *markRange = sender.markedTextRange;
    int pos = [sender offsetFromPosition:markRange.start
    toPosition:markRange.end];
    int nLength = sender.text.length - pos;
    if (nLength > 20 && pos == 0)
    sender.text = [sender.text substringToIndex:20];
}

 

posted @ 2014-10-24 13:29  菜鸟程序猿  阅读(187)  评论(0编辑  收藏  举报