{"id":6995,"date":"2025-05-30T13:51:44","date_gmt":"2025-06-05T06:18:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6995"},"modified":"2025-06-05T13:51:44","modified_gmt":"2025-06-05T06:18:43","slug":"https-badgameshow-com-steven-173","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-173\/","title":{"rendered":"Swift UITextField \u5bec\u5ea6\u8a2d\u5b9a\u8207\u6587\u5b57\u8f38\u5165\u6578\u91cf\u9650\u5236\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u6307\u5357"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField, \u8a2d\u7f6e\u5bec\u5ea6, \u9650\u5236\u6587\u5b57\u8f38\u5165\u6578\u91cf, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<h1>Swift UITextField \u5bec\u5ea6\u8a2d\u5b9a\u8207\u6587\u5b57\u8f38\u5165\u6578\u91cf\u9650\u5236\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u6307\u5357<\/h1>\n<p>\u5728\u958b\u767c iOS App \u6642\uff0c<strong>UITextField<\/strong> \u662f\u7528\u4f86\u8b93\u4f7f\u7528\u8005\u8f38\u5165\u6587\u5b57\u7684\u5e38\u7528\u5143\u4ef6\u3002\u5728\u8a2d\u8a08\u4e0a\uff0c\u6211\u5011\u53ef\u80fd\u9700\u8981\u8a2d\u5b9a <strong>UITextField<\/strong> \u7684\u5bec\u5ea6\uff0c\u4e26\u9650\u5236\u4f7f\u7528\u8005\u8f38\u5165\u7684\u6587\u5b57\u6578\u91cf\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8a2d\u5b9a <strong>UITextField<\/strong> \u7684\u5bec\u5ea6\uff0c\u4ee5\u53ca\u5982\u4f55\u9650\u5236\u6587\u5b57\u8f38\u5165\u7684\u6578\u91cf\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u5be6\u4f5c\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u8a2d\u5b9a UITextField \u5bec\u5ea6<\/h2>\n<p>\u8981\u8a2d\u5b9a <strong>UITextField<\/strong> \u7684\u5bec\u5ea6\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>frame<\/strong> \u5c6c\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 40))\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <strong>CGRect<\/strong> \u4f86\u8a2d\u5b9a <strong>UITextField<\/strong> \u7684\u5bec\u5ea6\uff0c\u9019\u88e1\u5c07\u5bec\u5ea6\u8a2d\u5b9a\u70ba 200\uff0c\u9ad8\u5ea6\u8a2d\u5b9a\u70ba 40\u3002\u9019\u500b\u65b9\u6cd5\u9069\u7528\u65bc\u9700\u8981\u7cbe\u78ba\u63a7\u5236 <strong>UITextField<\/strong> \u5c3a\u5bf8\u7684\u60c5\u6cc1\u3002<\/p>\n<h2>\u9650\u5236 UITextField \u6587\u5b57\u8f38\u5165\u6578\u91cf<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c<strong>UITextField<\/strong> \u4e26\u6c92\u6709\u5167\u5efa\u7684 <strong>maxLength<\/strong> \u5c6c\u6027\u3002\u6211\u5011\u9700\u8981\u900f\u904e\u4ee3\u7406\u65b9\u6cd5\u4f86\u5be6\u73fe\u5c0d\u6587\u5b57\u8f38\u5165\u6578\u91cf\u7684\u9650\u5236\u3002\u4ee5\u4e0b\u662f\u5177\u9ad4\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UITextFieldDelegate {\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \n        let textField = UITextField(frame: CGRect(x: 20, y: 100, width: 280, height: 40))\n        textField.borderStyle = .roundedRect\n        textField.delegate = self\n        self.view.addSubview(textField)\n    }\n\n    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n        let currentText = textField.text ?? \"\"\n        guard let stringRange = Range(range, in: currentText) else { return false }\n        let updatedText = currentText.replacingCharacters(in: stringRange, with: string)\n        return updatedText.count <= 10 \/\/ \u9650\u5236\u6700\u591a\u53ea\u80fd\u8f38\u5165 10 \u500b\u5b57\u5143\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u8a2d\u7f6e\u4e86\u4e00\u500b <strong>UITextField<\/strong> \u4e26\u5be6\u4f5c\u4e86 <code>UITextFieldDelegate<\/code> \u7684 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u4f86\u9650\u5236\u6700\u591a\u53ea\u80fd\u8f38\u5165 10 \u500b\u5b57\u5143\u3002\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u4f7f\u7528\u8005\u7684\u8f38\u5165\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u6587\u5b57\u6578\u91cf\u672a\u9650\u5236\uff1a<\/strong>\u8acb\u78ba\u8a8d\u4f60\u7684 UITextField \u7684 delegate \u662f\u5426\u5df2\u6b63\u78ba\u8a2d\u5b9a\u3002<\/li>\n<li><strong>\u986f\u793a\u4e0d\u6b63\u78ba\uff1a<\/strong>\u6aa2\u67e5\u4f60\u7684 frame \u8a2d\u5b9a\uff0c\u78ba\u4fdd UITextField \u5728\u8996\u5716\u4e2d\u53ef\u898b\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u8a2d\u5b9a\uff0c\u9084\u53ef\u4ee5\u8003\u616e\u70ba <strong>UITextField<\/strong> \u6dfb\u52a0\u6a23\u5f0f\u6216\u662f\u8f38\u5165\u9a57\u8b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u9a57\u8b49\u96fb\u5b50\u90f5\u4ef6\u6216\u96fb\u8a71\u865f\u78bc\u7684\u683c\u5f0f\uff0c\u63d0\u5347\u4f7f\u7528\u8005\u7684\u8f38\u5165\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITextField-\u8a2d\u7f6e\u5bec\u5ea6-\u9650\u5236\u6587\u5b57\u8f38\u5165\u6578\u91cf-\ud83d\udcdd_1675400226.1145012.webp\" alt=\"Swift UITextField \u8a2d\u7f6e\u5bec\u5ea6 \u9650\u5236\u6587\u5b57\u8f38\u5165\u6578\u91cf \ud83d\udcdd\" title=\"Swift UITextField, \u8a2d\u7f6e\u5bec\u5ea6, \u9650\u5236\u6587\u5b57\u8f38\u5165, \u6587\u5b57\u8f38\u5165\u6846, App\u5b8c\u5584\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8a2d\u5b9a <strong>UITextField<\/strong> \u7684\u5bec\u5ea6\u4ee5\u53ca\u9650\u5236\u4f7f\u7528\u8005\u7684\u6587\u5b57\u8f38\u5165\u6578\u91cf\u3002\u9019\u4e9b\u6280\u5de7\u5c0d\u65bc\u958b\u767c iOS App \u6642\u975e\u5e38\u5be6\u7528\uff0c\u80fd\u5e6b\u52a9\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>\u5982\u4f55\u5728 Swift \u4e2d\u8a2d\u7f6e UITextField \u7684\u908a\u6846\u6a23\u5f0f\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e\u8a2d\u5b9a <code>borderStyle<\/code> \u5c6c\u6027\u4f86\u6539\u8b8a UITextField \u7684\u908a\u6846\u6a23\u5f0f\uff0c\u4f8b\u5982\uff1a<code>textField.borderStyle = .roundedRect<\/code>\u3002<\/p>\n<h3>UITextField \u652f\u6301\u591a\u884c\u8f38\u5165\u55ce\uff1f<\/h3>\n<p>\u4e0d\u652f\u6301\uff0cUITextField \u4e3b\u8981\u7528\u65bc\u55ae\u884c\u8f38\u5165\u3002\u5982\u679c\u9700\u8981\u591a\u884c\u8f38\u5165\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <strong>UITextView<\/strong>\u3002<\/p>\n<p>```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728Swift\u4e2d\u8a2d\u7f6eUITextField\u7684\u5bec\u5ea6\u53ca\u9650\u5236\u6587\u5b57\u8f38\u5165\u6578\u91cf\uff0c\u4f7f\u7528Swift\u7684UITextField\u53ef\u4ee5\u8f15\u9b06\u8a2d\u7f6e\u6587\u5b57\u8f38\u5165\u6846\u7684\u5bec\u5ea6\u53ca\u9650\u5236\u6587\u5b57\u8f38\u5165\u6578\u91cf\uff0c\u8b93\u4f60\u7684App\u66f4\u52a0\u5b8c\u5584\u3002<\/p>\n","protected":false},"author":1,"featured_media":6993,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITextField-\u8a2d\u7f6e\u5bec\u5ea6-\u9650\u5236\u6587\u5b57\u8f38\u5165\u6578\u91cf-\ud83d\udcdd_1675400226.1145012.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1OP","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6995"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6995\/revisions"}],"predecessor-version":[{"id":13316,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6995\/revisions\/13316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6993"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}