{"id":7403,"date":"2022-09-16T14:54:27","date_gmt":"2022-09-16T06:54:27","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7403"},"modified":"2022-09-16T14:54:27","modified_gmt":"2022-09-16T06:54:27","slug":"swift-textview%e5%92%8ctextfield%e4%b9%8breturn%e5%9b%9e%e6%94%b6%e9%94%ae%e7%9b%98","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7403","title":{"rendered":"Swift &#8211; TextView\u548cTextField\u4e4breturn\u56de\u6536\u952e\u76d8"},"content":{"rendered":"\n<p>\u6211\u4eec\u90fd\u77e5\u9053android\u952e\u76d8\u9690\u85cf\u662f\u4e0d\u7528\u5f00\u53d1\u8005\u63a7\u5236\u7684\uff0c\u7528\u6237\u81ea\u5df1\u5c31\u53ef\u4ee5\u5173\u95ed\u9690\u85cf\uff0c\u800cios\u7684\u952e\u76d8\u5c31\u6bd4\u8f83\u90a3\u4e2a\u4e86\uff0c\u8fd8\u9700\u8981\u5f00\u53d1\u8005\u8bbe\u7f6e\u63a7\u4ef6\u53ef\u9690\u85cf\u624d\u80fd\u6536\u8d77\u952e\u76d8\u3002<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u70b9\u51fb\u754c\u9762\u7a7a\u767d\u5904\u5373\u53ef\u6536\u8d77\u952e\u76d8\uff0c\u7a7a\u767d\u5904\u4e0d\u80fd\u6709\u5176\u4ed6\u63a7\u4ef6\u7684\u54cd\u5e94\u4e8b\u4ef6\u3002<\/strong><\/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=\"\">\/\/\u70b9\u51fb\u7a7a\u767d\u5904\u5173\u95ed\u952e\u76d8\n    override func touchesEnded(_ touches: Set&lt;UITouch>, with event: UIEvent?) {\n        \/\/\u67d0\u4e2atextview\u5931\u53bb\u4e86\u54cd\u5e94\u8005\uff0c\u5373\u6536\u8d77\u952e\u76d8\u4e86\n        textView.resignFirstResponder()\n        \/\/\u6216\u6ce8\u9500\u5f53\u524dview(\u6216\u5b83\u4e0b\u5c5e\u5d4c\u5165\u7684text fields)\u7684first responder \u72b6\u6001\uff0c\u5373\u53ef\u5173\u95ed\u5176\u5b50\u63a7\u4ef6\u952e\u76d8\n        self.view?.endEditing(false)\n    }\n<\/pre>\n\n\n\n<p>\u4e8c\u3001<strong>\u70b9\u51fb\u952e\u76d8return\u5904\u5373\u53ef\u6536\u8d77\u952e\u76d8\u3002<\/strong>\u9996\u5148\uff0c\u5f15\u7528UITextFieldDelegate\u7c7b<\/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=\"\">    override func viewDidLoad() {\n        super.viewDidLoad()\n        textField.delegate = self\n        \/\/\u952e\u76d8\u201creturn\u201d\u53d8\u6210\u201c\u5b8c\u6210\u201d\n        textField.returnKeyType = UIReturnKeyType.done\n    }\n \n    \/\/textField\u70b9\u51fbreturn\u5173\u95ed\u952e\u76d8\n    func textFieldShouldReturn(_ textField: UITextField) -> Bool {\n        self.view?.endEditing(false)\n        return true\n    }<\/pre>\n\n\n\n<p>\u4e09\u3001<strong>\u70b9\u51fb\u952e\u76d8return\u5904\u5373\u53ef\u6536\u8d77\u952e\u76d8\u3002<\/strong>\u9996\u5148\uff0c\u5f15\u7528UITextViewDelegate\u7c7b\uff0c\u5728UITextView\u7684\u4ee3\u7406\u65b9\u6cd5\u00a0<strong>replacementText<\/strong>\u00a0\u4e2d\u83b7\u53d6\u6362\u884c\u5b57\u7b26(\u4e5f\u5c31\u662f &#8220;<strong>\\n<\/strong>&#8220;)\uff0c\u5224\u65ad\u662f\u5426\u662f\u6362\u884c\u5b57\u7b26\u6765\u8fdb\u884c\u5904\u7406\u3002<\/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=\"\">    override func viewDidLoad() {\n        super.viewDidLoad()\n \n        textView.delegate = self\n        \/\/\u952e\u76d8\u201creturn\u201d\u53d8\u6210\u201c\u5b8c\u6210\u201d\n        textView.returnKeyType = UIReturnKeyType.done\n    }\n \n    \/\/textView\u70b9\u51fbreturn\uff08\u6362\u884c\uff09\u5173\u95ed\u952e\u76d8\n     func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {\n        if text == \"\\n\" {\n            self.view?.endEditing(false)\n            return false\n        }\n \n        \/\/\u5b57\u6570\u9650\u5236\uff0c\u5728\u8fd9\u91cc\u6211\u7684\u5904\u7406\u662f\u7ed9\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63d0\u793a\uff0c\n        if range.location >= 30 {\n            print(\"\u8d85\u8fc730\u5b57\u6570\u4e86\")\n            return false\n        }\n        return true\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053android\u952e\u76d8\u9690\u85cf\u662f\u4e0d\u7528\u5f00\u53d1\u8005\u63a7\u5236\u7684\uff0c\u7528\u6237\u81ea\u5df1\u5c31\u53ef\u4ee5\u5173\u95ed\u9690\u85cf\uff0c\u800cios\u7684\u952e\u76d8\u5c31\u6bd4\u8f83\u90a3\u4e2a\u4e86\uff0c\u8fd8\u9700\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7403"}],"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=7403"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7403\/revisions"}],"predecessor-version":[{"id":7404,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7403\/revisions\/7404"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}