{"id":1085,"date":"2020-09-10T15:00:13","date_gmt":"2020-09-10T07:00:13","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1085"},"modified":"2021-05-19T20:30:31","modified_gmt":"2021-05-19T12:30:31","slug":"swiftui-uitextfield-%e5%8f%aa%e8%be%93%e5%85%a5%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1085","title":{"rendered":"SwiftUI UITextField \u53ea\u8f93\u5165\u6570\u5b57"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct CustomTextfield: UIViewRepresentable {\n\n    @Binding var text: String\n\n    func makeUIView(context: Context) -> UITextField {\n        let view = UITextField()\n        view.setLeftPaddingPoints(10)\n        view.isUserInteractionEnabled = true\n        view.backgroundColor = .white\n        view.layer.borderColor = uiColorE5E5E5.cgColor\n        view.layer.borderWidth = 1\n        view.layer.cornerRadius = 4\n        view.font = UIFont.systemFont(ofSize: 16, weight: .bold)\n        view.textColor = uiColorD57B21\n        view.delegate = context.coordinator\n        view.keyboardType = UIKeyboardType.numberPad\n\n        return view\n    }\n\n    func updateUIView(_ uiView: UITextField, context: Context) {\n        uiView.text = text\n    }\n\n    func makeCoordinator() -> CustomTextfield.Coordinator {\n        Coordinator(self)\n    }\n\n    class Coordinator: NSObject, UITextFieldDelegate {\n        var control: CustomTextfield\n\n        init(_ control: CustomTextfield) {\n            self.control = control\n        }\n\n        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {\n            self.control.text = textField.text!\n            return true\n        }\n\n     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n        \/\/ \u83b7\u53d6\u8f93\u5165\u7684\u6587\u672c\uff0c\u79fb\u9664\u5411\u8f93\u5165\u6846\u4e2d\u7c98\u8d34\u6587\u672c\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u52a0\u4e0a\u7684\u7a7a\u683c\uff08iOS11\u4e0a\u6709\u8be5\u95ee\u9898\uff09\n        let new = string.replacingOccurrences(of: \" \", with: \"\")\n        \/\/ \u83b7\u53d6\u7f16\u8f91\u524d\u7684\u6587\u672c\n        var old = NSString(string: textField.text ?? \"\")\n        \/\/ \u83b7\u53d6\u7f16\u8f91\u540e\u7684\u6587\u672c\n        old = old.replacingCharacters(in: range, with: new) as NSString\n        \/\/ \u83b7\u53d6\u6570\u5b57\u7684\u5b57\u7b26\u96c6\n        let number = CharacterSet(charactersIn: \"0123456789\")\n        \/\/ \u5224\u65ad\u7f16\u8f91\u540e\u7684\u6587\u672c\u662f\u5426\u5168\u4e3a\u6570\u5b57\n        if old.rangeOfCharacter(from: number.inverted).location == NSNotFound {\n            \/\/ number.inverted\u8868\u793a\u9664\u4e86number\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u4ee5\u5916\u7684\u5176\u4ed6\u5168\u90e8\u5b57\u7b26\n            \/\/ \u5982\u679cold\u4e2d\u4e0d\u5305\u542b\u5176\u4ed6\u5b57\u7b26\uff0c\u5219\u683c\u5f0f\u6b63\u786e\n            \/\/ \u5141\u8bb8\u672c\u6b21\u7f16\u8f91\n            textField.text = old as String\n            \/\/ \u79fb\u52a8\u5149\u6807\u7684\u4f4d\u7f6e\n            DispatchQueue.main.async {\n                let beginning = textField.beginningOfDocument\n                let position = textField.position(from: beginning, offset: range.location + new.count)!\n                textField.selectedTextRange = textField.textRange(from: position, to: position)\n            }\n        }\n        return false\n    }\n\n}<\/pre>\n\n\n\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u53ea\u80fd\u8f93\u5165\u5c0f\u6570\uff0c\u6bd4\u598238.12211<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct gpsTextfield: UIViewRepresentable {\n    @Binding var text: String\n\n    func makeUIView(context: Context) -> UITextField {\n        let textfield = UITextField()\n        textfield.keyboardType = .numberPad\n        textfield.textAlignment = .left\n        textfield.delegate = context.coordinator\n        textfield.text = text\n        textfield.backgroundColor = uiColorF9F9F9\n        textfield.layer.borderColor = uiColorC5C5C5.cgColor\n        textfield.layer.cornerRadius = 4\n        textfield.layer.borderWidth = 1\n        textfield.font = UIFont.init(name: \"KozGoPro-Bold\", size: 16)\n        textfield.setLeftPaddingPoints(10)\n\n        return textfield\n    }\n\n    func updateUIView(_ uiView: UITextField, context: Context) {\n        uiView.text = text\n    }\n\n    func makeCoordinator() -> gpsTextfield.Coordinator {\n        Coordinator(self)\n    }\n\n    class Coordinator: NSObject, UITextFieldDelegate {\n        var control: gpsTextfield\n\n        init(_ control: gpsTextfield) {\n            self.control = control\n        }\n\n        func textFieldDidChangeSelection(_ textField: UITextField) {\n            self.control.text = textField.text!\n        }\n\n        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {\n            NotificationCenter.default.post(name: .kmpointEditBegin, object: nil, userInfo: nil)\n            return true\n        }\n\n        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {\n            self.control.text = textField.text!\n            textField.endEditing(true)\n            return true\n        }\n\n        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n            \/\/ \u83b7\u53d6\u8f93\u5165\u7684\u6587\u672c\uff0c\u79fb\u9664\u5411\u8f93\u5165\u6846\u4e2d\u7c98\u8d34\u6587\u672c\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u52a0\u4e0a\u7684\u7a7a\u683c\uff08iOS11\u4e0a\u6709\u8be5\u95ee\u9898\uff09\n            let new = string.replacingOccurrences(of: \" \", with: \"\")\n            \/\/ \u83b7\u53d6\u7f16\u8f91\u524d\u7684\u6587\u672c\n            var old = NSString(string: textField.text ?? \"\")\n            \/\/ \u83b7\u53d6\u7f16\u8f91\u540e\u7684\u6587\u672c\n            old = old.replacingCharacters(in: range, with: new) as NSString\n            \/\/ \u83b7\u53d6\u6570\u5b57\u7684\u5b57\u7b26\u96c6\n            let number = CharacterSet(charactersIn: \"0123456789.\")\n            \/\/ \u5224\u65ad\u7f16\u8f91\u540e\u7684\u6587\u672c\u662f\u5426\u5168\u4e3a\u6570\u5b57\n            if old.rangeOfCharacter(from: number.inverted).location == NSNotFound &amp;&amp; CommonUtil.checkLocationInput(gpsString: old as String) {\n                \/\/ number.inverted\u8868\u793a\u9664\u4e86number\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u4ee5\u5916\u7684\u5176\u4ed6\u5168\u90e8\u5b57\u7b26\n                \/\/ \u5982\u679cold\u4e2d\u4e0d\u5305\u542b\u5176\u4ed6\u5b57\u7b26\uff0c\u5219\u683c\u5f0f\u6b63\u786e\n                \/\/ \u5141\u8bb8\u672c\u6b21\u7f16\u8f91\n                textField.text = old as String\n                \/\/ \u79fb\u52a8\u5149\u6807\u7684\u4f4d\u7f6e\n                DispatchQueue.main.async {\n                    let beginning = textField.beginningOfDocument\n                    let position = textField.position(from: beginning, offset: range.location + new.count)!\n                    textField.selectedTextRange = textField.textRange(from: position, to: position)\n                }\n            }\n            return false\n        }\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/ \u5224\u65ad\u5c0f\u6570\u70b9\u4e2a\u6570\npublic class func checkLocationInput(gpsString: String) -> Bool {\n        \n        var num = 0\n        for ch in gpsString\n        {\n            if ch == \".\" {\n                num = num + 1\n            }\n        }\n        if num > 1 {\n            return false\n        }\n        return true\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u53ea\u80fd\u8f93\u5165\u5c0f\u6570\uff0c\u6bd4\u598238.12211<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1085"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1085"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions"}],"predecessor-version":[{"id":2810,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions\/2810"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}