加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift TextField 金额输入

发布时间:2020-12-14 04:25:58 所属栏目:百科 来源:网络整理
导读:1 // MARK: - UITextFieldDelegate 2 func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) - Bool { 3 guard string == " . " || string == " 0 " else { 4 //限制输入个数{0,2},0-2 5 /*
 1 // MARK: - UITextFieldDelegate
 2     func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool {
 3         guard string == "." || string == "0" else {
 4             //限制输入个数{0,2},0-2
 5             /*
 6              let newString = (textField.text! as NSString).replacingCharacters(in: range,with: string)
 7              
 8              //纯整数,限制只有6位{0,6}。如果是小数,就小数点前6后8位{0,8}
 9              //let expression = "^[0-9]{0,6}?$*((.|,)[0-9]{0,8})?$" 
10              let expression = "^[0-9]*((.|,2})?$"
11              let regex = try! NSRegularExpression(pattern: expression,options: NSRegularExpression.Options.allowCommentsAndWhitespace)
12              let numberOfMatches = regex.numberOfMatches(in: newString,options:.reportProgress,range: NSMakeRange(0,(newString as NSString).length))
13              return numberOfMatches != 0
14              */
15             
16             //无限制
17             return true
18         }
19         
20         guard let text = textField.text else { return true }
21         if text.count == 0 {
22             textField.text = "0."
23             return false
24         }
25         
26         if text.range(of: ".") != nil && string == "." {
27             return false
28         }
29         
30         return true
31     }     

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读