您好,登录后才能下订单哦!
@interface ViewController ()<UITextFieldDelegate>
{
UITextField * _numberFielde;
}
@end
@implementation ViewController
static NSString * NUMBER = @"0123456789";//数字
static int setNumber = 1; //几个数字加空格 1改成自己想要的字符
static NSString * kongSring = @"*";//几个数字中间加的字符 x改成自己想要的字符
- (void)viewDidLoad {
[super viewDidLoad];
_numberFielde = [[UITextField alloc] initWithFrame:CGRectMake(10, 100,self.view.frame.size.width-20, 35)];
_numberFielde.layer.borderColor = [UIColor grayColor].CGColor;
_numberFielde.layer.borderWidth = 1.0f;
_numberFielde.delegate = self;
[_numberFielde becomeFirstResponder];
_numberFielde.placeholder = @"请输入数字";
[self.view addSubview:_numberFielde];
// Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:NUMBER] invertedSet]; //定义一个字符集合
NSString*filtered = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if(basicTest)
{
if (textField == _numberFielde)
{
//setNumber位加一个空格
if ([string isEqualToString:@""]){ // 删除字符
if ((textField.text.length - 2) % setNumber == 0) {
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
return YES;
} else {
if (textField.text.length % setNumber == 0) {
textField.text = [NSString stringWithFormat:@"%@%@", textField.text,kongSring];
}
}
return YES;
}
}
else
{
return NO;
}
return YES;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。