- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
textF.backgroundColor = [UIColor grayColor];
[self.view addSubview:textF];
self.textF = textF;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextField:) name:@"UITextFieldTextDidChangeNotification" object:textF];
}
- (void)limitTextField:(NSNotification *)note {
int limit = 5;
if ([self.textF.text length] > limit) {
[self.textF setText:[self.textF.text substringToIndex:limit]];
}
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}如何限制文本框输入的个数
原文:http://blog.csdn.net/u010438187/article/details/46368659