{"id":238,"date":"2020-07-27T12:04:47","date_gmt":"2020-07-27T04:04:47","guid":{"rendered":"http:\/\/123.57.164.21\/?p=238"},"modified":"2020-08-05T12:18:16","modified_gmt":"2020-08-05T04:18:16","slug":"swiftui-%e8%87%aa%e5%ae%9a%e4%b9%89-textfield","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=238","title":{"rendered":"SwiftUI \u81ea\u5b9a\u4e49 Textfield"},"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=\"\">truct CustomTextfield: UIViewRepresentable {\n    @Binding var text: String\n    var keyType: UIKeyboardType\n    func makeUIView(context: Context) -> UITextField {\n        let textfield = UITextField()\n        textfield.keyboardType = keyType\n        textfield.textAlignment = .center\n        textfield.delegate = context.coordinator\n\n        let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))\n        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)\n        let doneButton = UIBarButtonItem(title: \"\u78ba\u5b9a\", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))\n        toolBar.items = [flexibleSpace, doneButton]\n        toolBar.setItems([flexibleSpace, doneButton], animated: true)\n        textfield.inputAccessoryView = toolBar\n        return textfield\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}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/238"}],"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=238"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":240,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions\/240"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}