如何限制textField中输入字符的个数

如题所述

//创建
1.
phonetext = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width-width, 25)];
...... //设置相关属性

2.关联上selector. 注意: 事件要forControlEvents:UIControlEventEditingChanged
[phonetext addTarget:self action:@selector(changePhoneNumValue) forControlEvents:UIControlEventEditingChanged];//手机号码

3.实现selector
-(void)changePhoneNumValue
{
int MaxLen = 15;
NSString* szText = [phonetext text];
if ([phonetext length]> MaxLen)
{
phonetext.text = [szText substringToIndex:MaxLen];
}
}
温馨提示:答案为网友推荐,仅供参考