{"id":7104,"date":"2025-05-31T14:36:59","date_gmt":"2025-06-05T07:29:59","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7104"},"modified":"2025-06-05T14:36:59","modified_gmt":"2025-06-05T07:29:59","slug":"https-badgameshow-com-steven-227","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-227\/","title":{"rendered":"\u9650\u5236 UITextField \u8f38\u5165\u9577\u5ea6\u7684\u6700\u4f73\u5be6\u8e10\u8207 Swift \u7a0b\u5f0f\u78bc\u793a\u7bc4 | 2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField, \u9650\u5236\u8f38\u5165\u9577\u5ea6, iOS\u958b\u767c, Swift\u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h2>\u7c21\u4ecb<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u65b0\u8208\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5feb\u901f\u958b\u767c iOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u4f7f\u7528 UITextField \u4f86\u9650\u5236\u4f7f\u7528\u8005\u8f38\u5165\u7684\u9577\u5ea6\u662f\u5e38\u898b\u9700\u6c42\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8f15\u9b06\u5be6\u73fe\u9019\u500b\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 UITextFieldDelegate \u9650\u5236\u8f38\u5165\u9577\u5ea6<\/h2>\n<p>\u900f\u904e\u5be6\u4f5c <strong>UITextFieldDelegate<\/strong> \u5354\u5b9a\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236 UITextField \u7684\u8f38\u5165\u9577\u5ea6\u3002\u4ee5\u4e0b\u662f\u5be6\u4f5c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u78ba\u4fdd\u4f60\u7684 UIViewController \u7b26\u5408 <strong>UITextFieldDelegate<\/strong> \u5354\u5b9a\u3002<\/li>\n<li>\u5c07 UITextField \u7684 delegate \u8a2d\u70ba\u7576\u524d\u7684 ViewController\u3002<\/li>\n<li>\u5be6\u4f5c <strong>shouldChangeCharactersIn<\/strong> \u65b9\u6cd5\u4f86\u63a7\u5236\u8f38\u5165\u9577\u5ea6\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UITextFieldDelegate {\n    @IBOutlet weak var textField: UITextField!\n    \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        textField.delegate = self\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\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u6703\u5728\u4f7f\u7528\u8005\u8f38\u5165\u6587\u5b57\u6642\u6aa2\u67e5\u6587\u5b57\u9577\u5ea6\uff0c\u5982\u679c\u8d85\u904e 10 \u500b\u5b57\u5143\uff0c\u5c31\u6703\u963b\u6b62\u8f38\u5165\u3002<\/p>\n<h2>\u4f7f\u7528 maxLength \u5c6c\u6027\u9650\u5236\u8f38\u5165\u9577\u5ea6<\/h2>\n<p>\u5728 Swift \u4e2d\uff0cUITextField \u4e26\u6c92\u6709\u5167\u5efa\u7684 <strong>maxLength<\/strong> \u5c6c\u6027\uff0c\u4f46\u53ef\u4ee5\u900f\u904e\u64f4\u5c55\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u5be6\u4f5c\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension UITextField {\n    @IBInspectable var maxLength: Int {\n        get {\n            return self.maxLength\n        }\n        set {\n            self.addTarget(self, action: #selector(limitLength), for: .editingChanged)\n        }\n    }\n\n    @objc private func limitLength() {\n        if let text = self.text, text.count > maxLength {\n            self.text = String(text.prefix(maxLength))\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b UITextField \u7684\u64f4\u5c55\uff0c\u7576\u8f38\u5165\u8d85\u904e\u8a2d\u5b9a\u7684\u9577\u5ea6\u6642\uff0c\u81ea\u52d5\u622a\u65b7\u591a\u9918\u7684\u6587\u5b57\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u9650\u5236 UITextField \u7684\u8f38\u5165\u9577\u5ea6\u975e\u5e38\u7c21\u55ae\u3002\u7121\u8ad6\u662f\u4f7f\u7528 UITextFieldDelegate \u9084\u662f\u81ea\u5b9a\u7fa9\u5c6c\u6027\uff0c\u958b\u767c\u8005\u90fd\u80fd\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u9019\u500b\u529f\u80fd\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITextField-\u9650\u5236\u8f38\u5165\u9577\u5ea6-\ud83d\udcdd_1675402622.647603.webp\" alt=\"Swift UITextField \u9650\u5236\u8f38\u5165\u9577\u5ea6 \ud83d\udcdd\" title=\"Swift UITextField, \u9650\u5236\u8f38\u5165\u9577\u5ea6, UITextField\u8f38\u5165\u9577\u5ea6, \u9650\u5236UITextField\u8f38\u5165, Swift\u8a9e\u8a00\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u8a2d\u5b9a UITextField \u7684 delegate\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728 viewDidLoad \u65b9\u6cd5\u4e2d\u5c07 UITextField \u7684 delegate \u8a2d\u7f6e\u70ba\u7576\u524d\u7684 ViewController\uff0c\u4f8b\u5982\uff1a<code>textField.delegate = self<\/code>\u3002<\/p>\n<h3>2. \u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u9650\u5236\u8f38\u5165\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u53ef\u4ee5\u5728 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u4e2d\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u9032\u4e00\u6b65\u904e\u6ffe\u8f38\u5165\u5167\u5bb9\u3002<\/p>\n<h3>3. \u9664\u4e86\u9650\u5236\u9577\u5ea6\uff0c\u9084\u53ef\u4ee5\u505a\u54ea\u4e9b\u8f38\u5165\u9a57\u8b49\uff1f<\/h3>\n<p>\u9664\u4e86\u9577\u5ea6\uff0c\u9084\u53ef\u4ee5\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u5143\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\uff0c\u4f8b\u5982\u6578\u5b57\u3001\u5b57\u6bcd\u6216\u7279\u5b9a\u7b26\u865f\u7b49\u3002<\/p>\n<p>```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728Swift\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528UITextField\u7684shouldChangeCharactersInRange\u65b9\u6cd5\u4f86\u9650\u5236UITextField\u8f38\u5165\u7684\u9577\u5ea6\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u63a7\u5236\u8f38\u5165\u7684\u5b57\u6578\uff0c\u4ee5\u7b26\u5408\u61c9\u7528\u7a0b\u5f0f\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u9650\u5236UITextField\u8f38\u5165\u9577\u5ea6\u3002<\/p>\n","protected":false},"author":1,"featured_media":7103,"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-7104","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-\u9650\u5236\u8f38\u5165\u9577\u5ea6-\ud83d\udcdd_1675402622.647603.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1QA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7104","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=7104"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7104\/revisions"}],"predecessor-version":[{"id":13536,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7104\/revisions\/13536"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7103"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}