1、iOS仿支付宝、微信支付键盘输入,废话不多说,直接上图:
iOS仿支付宝、微信支付键盘输入.png
2、一言不合就撸代码:
代码1
_moneyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, kScreen_Width - 20, 40)];
_moneyTextField.placeholder = @"¥请输入充值金额";
_moneyTextField.keyboardType = UIKeyboardTypeDecimalPad;
_moneyTextField.textAlignment = NSTextAlignmentCenter;
_moneyTextField.font = [UIFont systemFontOfSize:20];
_moneyTextField.delegate = self;
[self.view addSubview:_moneyTextField];
代码2
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = _moneyTextField.text;
NSString *decimalSeperator = @".";
NSCharacterSet *charSet = nil;
NSString *numberChars = @"0123456789";
if ([string isEqualToString:decimalSeperator] && [text length] == 0) {
return NO;
}
NSRange decimalRange = [text rangeOfString:decimalSeperator];
BOOL isDecimalNumber = (decimalRange.location != NSNotFound);
if (isDecimalNumber) {
charSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];
if ([string rangeOfString:decimalSeperator].location != NSNotFound) {
return NO;
}
}
else {
numberChars = [numberChars stringByAppendingString:decimalSeperator];
charSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];
}
NSCharacterSet *invertedCharSet = [charSet invertedSet];
NSString *trimmedString = [string stringByTrimmingCharactersInSet:invertedCharSet];
text = [text stringByReplacingCharactersInRange:range withString:trimmedString];
if (isDecimalNumber) {
NSArray *arr = [text componentsSeparatedByString:decimalSeperator];
if ([arr count] == 2) {
if ([arr[1] length] > 2) {
return NO;
}
}
}
textField.text = text;
return NO;
}
3、是不是很💖动,赶快新建一个工程试试吧~
网友评论