限制UITextView 只能输入数字

在有些情况下,需要限制UITextView只能限制输入数字,特别是在填写表格的时候,能够预防数据的错误录入,实现改功能比较简单,只需要实现

– (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@”0123456789″];
for (int i = 0; i < [ text length ]; i++) {
unichar c = [ text characterAtIndex:i];
if ([myCharSet characterIsMember:c])
{
return YES;
}
}
return NO;
}

This entry was posted in iOS.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">