{"id":7356,"date":"2025-06-02T12:42:53","date_gmt":"2025-06-05T04:54:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7356"},"modified":"2025-06-05T12:42:53","modified_gmt":"2025-06-05T04:54:53","slug":"https-badgameshow-com-steven-349","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-349\/","title":{"rendered":"Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u9650\u5236 UITextField \u8f38\u5165\u5b57\u6578\u7684\u6700\u4f73\u5be6\u8e10\u30102025 \u6700\u65b0\u8a9e\u6cd5\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField, \u8f38\u5165\u5b57\u6578\u9650\u5236, iOS \u958b\u767c\"><\/p>\n<h2>\u5728 iOS App \u4e2d\u9650\u5236 UITextField \u8f38\u5165\u5b57\u6578\u7684\u91cd\u8981\u6027<\/h2>\n<p>\u5728\u958b\u767c iOS App \u7684\u904e\u7a0b\u4e2d\uff0c\u5408\u7406\u5730\u9650\u5236\u4f7f\u7528\u8005\u7684\u8f38\u5165\u5b57\u6578\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u8005\u8a3b\u518a\u5e33\u865f\u6216\u8a2d\u5b9a\u5bc6\u78bc\u7684\u60c5\u5883\u4e0b\u3002\u4f8b\u5982\uff0c\u5e33\u865f\u53ea\u80fd\u8f38\u5165 10 \u500b\u5b57\uff0c\u6216\u662f\u5bc6\u78bc\u9650\u5236\u5728 8 \u500b\u5b57\u5167\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\uff0c\u9084\u80fd\u907f\u514d\u56e0\u70ba\u8f38\u5165\u904e\u591a\u800c\u7522\u751f\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u9650\u5236 UITextField \u8f38\u5165\u5b57\u6578\u7684\u5be6\u4f5c\u6b65\u9a5f<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u900f\u904e\u5be6\u4f5c <strong>UITextFieldDelegate<\/strong> \u4f86\u9054\u6210\u9019\u500b\u76ee\u7684\u3002\u4ee5\u4e0b\u662f\u5177\u9ad4\u7684\u6b65\u9a5f\u548c\u7bc4\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5728 ViewController \u4e2d\uff0c\u5148\u5c07 UITextFieldDelegate \u8a2d\u5b9a\u70ba self\nclass ViewController: UIViewController, UITextFieldDelegate {\n\n    @IBOutlet weak var textField: UITextField! \/\/ \u9023\u63a5\u5230 Storyboard \u4e2d\u7684 UITextField\n\n    \/\/ \u5728 viewDidLoad \u4e2d\uff0c\u5c07 UITextField \u7684 delegate \u8a2d\u5b9a\u70ba self\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        textField.delegate = self\n    }\n\n    \/\/ \u5728 shouldChangeCharactersIn \u65b9\u6cd5\u4e2d\uff0c\u8a2d\u5b9a UITextField \u8f38\u5165\u5b57\u6578\u7684\u9650\u5236\n    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n        guard let text = textField.text else { return true }\n        let newLength = text.count + string.count - range.length\n        return newLength <= 10 \/\/ \u9650\u5236\u9577\u5ea6\u70ba 10\n    }\n}\n<\/code><\/pre>\n<h2>\u5b8c\u6574\u6027\u6aa2\u67e5\u8207\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u6642\uff0c\u8acb\u78ba\u4fdd\u4ee5\u4e0b\u5e7e\u9ede\uff0c\u4ee5\u907f\u514d\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd UITextField \u7684 delegate \u5df2\u6b63\u78ba\u8a2d\u7f6e\u70ba ViewController\u3002<\/li>\n<li>\u6aa2\u67e5 UITextField \u7684\u9023\u63a5\u662f\u5426\u6b63\u78ba\uff08\u5982\u679c\u4f7f\u7528 Storyboard\uff09\u3002<\/li>\n<li>\u5728\u9032\u884c\u52d5\u614b\u6e2c\u8a66\u6642\uff0c\u78ba\u4fdd\u6aa2\u67e5\u6240\u6709\u53ef\u80fd\u7684\u908a\u754c\u60c5\u6cc1\uff0c\u4f8b\u5982\u522a\u9664\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528\uff1a\u591a\u7a2e\u5b57\u6578\u9650\u5236\u7684\u60c5\u5883<\/h2>\n<p>\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u5230\u5176\u4ed6\u5834\u666f\uff0c\u4f8b\u5982\u5728\u4e0d\u540c\u7684 UITextField \u4e2d\u8a2d\u7f6e\u4e0d\u540c\u7684\u5b57\u6578\u9650\u5236\u3002\u60a8\u53ef\u4ee5\u6839\u64da UITextField \u7684\u6a19\u7c64\u6216\u6a19\u8b58\u7b26\u4f86\u8a2d\u7f6e\u76f8\u61c9\u7684\u9650\u5236\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n    guard let text = textField.text else { return true }\n    let newLength = text.count + string.count - range.length\n    \n    \/\/ \u6839\u64da\u4e0d\u540c\u7684 UITextField \u8a2d\u5b9a\u4e0d\u540c\u7684\u5b57\u6578\u9650\u5236\n    if textField == usernameTextField {\n        return newLength <= 10 \/\/ \u4f7f\u7528\u8005\u540d\u7a31\u9650\u5236\n    } else if textField == passwordTextField {\n        return newLength <= 8 \/\/ \u5bc6\u78bc\u9650\u5236\n    }\n    return true \/\/ \u5176\u4ed6\u60c5\u6cc1\u4e0b\u5141\u8a31\u6240\u6709\u8f38\u5165\n}\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u9650\u5236UITextField\u8f38\u5165\u5b57\u6578-\ud83d\udcdd_1675474739.6616151.webp\" alt=\"Swift \u9650\u5236UITextField\u8f38\u5165\u5b57\u6578 \ud83d\udcdd\" title=\"Swift\u9650\u5236\u8f38\u5165\u5b57\u6578, UITextField\u9650\u5236\u5b57\u6578, Swift\u9650\u5236\u6587\u5b57\u9577\u5ea6, Swift\u9650\u5236\u8f38\u5165\u9577\u5ea6, Swift\u9650\u5236\u8f38\u5165\u5b57\u5143\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&A)<\/h2>\n<h3>1. \u5982\u4f55\u5728 Swift \u4e2d\u6aa2\u67e5 UITextField \u7684\u7576\u524d\u5b57\u6578\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>textField.text?.count<\/code> \u4f86\u7372\u53d6\u7576\u524d\u5b57\u6578\u3002\u4f8b\u5982\uff1a<code>let currentLength = textField.text?.count ?? 0<\/code><\/p>\n<h3>2. \u5982\u679c\u6211\u9700\u8981\u5728 UITextField \u4e2d\u5141\u8a31\u7a7a\u683c\uff0c\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u4e2d\u6aa2\u67e5 <code>replacementString<\/code> \u662f\u5426\u5305\u542b\u7a7a\u683c\uff0c\u7136\u5f8c\u6c7a\u5b9a\u662f\u5426\u5141\u8a31\u8a72\u8f38\u5165\u3002<\/p>\n<h3>3. \u5982\u4f55\u91dd\u5c0d\u4e0d\u540c\u7684 UITextField \u8a2d\u5b9a\u4e0d\u540c\u7684\u5b57\u6578\u9650\u5236\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>if<\/code> \u689d\u4ef6\u8a9e\u53e5\u4f86\u6aa2\u67e5 <code>textField<\/code> \u7684\u6a19\u8b58\u7b26\uff0c\u4e26\u6839\u64da\u4e0d\u540c\u7684 UITextField \u8a2d\u5b9a\u4e0d\u540c\u7684\u9650\u5236\u3002<\/p>\n<p>```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528methods()\u51fd\u6578\u4f86\u9650\u5236UITextField\u8f38\u5165\u5b57\u6578\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8f38\u5165\u7684\u6587\u5b57\u9577\u5ea6\uff0c\u4ee5\u9054\u5230\u66f4\u597d\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":7355,"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-7356","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-\u9650\u5236UITextField\u8f38\u5165\u5b57\u6578-\ud83d\udcdd_1675474739.6616151.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1UE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7356","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=7356"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7356\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7355"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}