您的位置:首页 > 移动开发 > 微信开发

iOS仿支付宝、微信支付键盘输入

2016-10-21 07:39 507 查看

1、iOS仿支付宝、微信支付键盘输入,废话不多说,直接上图:



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、是不是很心动,赶快新建一个工程试试吧

文章转自 小猪也浪漫的简书
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息