{"id":7334,"date":"2025-05-31T12:48:14","date_gmt":"2025-06-05T04:49:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7334"},"modified":"2025-06-05T12:48:14","modified_gmt":"2025-06-05T04:49:13","slug":"https-badgameshow-com-steven-338","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-338\/","title":{"rendered":"Swift \u6587\u672c\u6846\uff1a\u5982\u4f55\u5728 2025 \u5e74\u9650\u5236\u6587\u672c\u9577\u5ea6\u7684\u6700\u4f73\u5be6\u8e10 \ud83d\udcdd"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField, \u9650\u5236\u6587\u672c\u9577\u5ea6, Swift \u6559\u5b78\"><\/p>\n<h1>Swift \u6587\u672c\u6846\uff1a\u5982\u4f55\u5728 2025 \u5e74\u9650\u5236\u6587\u672c\u9577\u5ea6\u7684\u6700\u4f73\u5be6\u8e10 \ud83d\udcdd<\/h1>\n<p>\u5728 Swift \u958b\u767c\u4e2d\uff0c\u6587\u672c\u6846\u662f\u7528\u6236\u8f38\u5165\u6587\u5b57\u7684\u4e3b\u8981\u65b9\u5f0f\u4e4b\u4e00\u3002\u70ba\u4e86\u63d0\u5347\u7528\u6236\u9ad4\u9a57\uff0c\u7d93\u5e38\u9700\u8981\u5c0d\u7528\u6236\u7684\u8f38\u5165\u9032\u884c\u9650\u5236\uff0c\u4f8b\u5982\u9650\u5236\u6587\u672c\u9577\u5ea6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 <strong>UITextField<\/strong> \u548c\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002<\/p>\n<h2>\u4f7f\u7528 UITextField \u5275\u5efa\u6587\u672c\u6846<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u5275\u5efa\u4e00\u500b\u6587\u672c\u6846\u4e26\u8a2d\u7f6e\u6700\u5927\u8f38\u5165\u9577\u5ea6\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">import UIKit\n\nclass ViewController: UIViewController {\n    let textField = UITextField()\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        setupTextField()\n    }\n\n    private func setupTextField() {\n        textField.borderStyle = .roundedRect\n        textField.placeholder = \"\u8acb\u8f38\u5165\u6587\u672c\uff08\u6700\u591a 10 \u500b\u5b57\u7b26\uff09\"\n        textField.delegate = self\n        textField.translatesAutoresizingMaskIntoConstraints = false\n        view.addSubview(textField)\n\n        \/\/ \u8a2d\u7f6e\u7d04\u675f\n        NSLayoutConstraint.activate([\n            textField.centerXAnchor.constraint(equalTo: view.centerXAnchor),\n            textField.centerYAnchor.constraint(equalTo: view.centerYAnchor),\n            textField.widthAnchor.constraint(equalToConstant: 300)\n        ])\n    }\n}\n<\/code><\/pre>\n<h2>\u9650\u5236\u6587\u672c\u9577\u5ea6\u7684\u65b9\u6cd5<\/h2>\n<p>\u70ba\u4e86\u9650\u5236\u6587\u672c\u6846\u7684\u8f38\u5165\u9577\u5ea6\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UITextFieldDelegate<\/strong> \u7684 <strong>shouldChangeCharactersIn<\/strong> \u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u5177\u9ad4\u5be6\u73fe\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension ViewController: UITextFieldDelegate {\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        \n        let updatedText = currentText.replacingCharacters(in: stringRange, with: string)\n        \n        \/\/ \u9650\u5236\u6700\u5927\u5b57\u7b26\u6578\u70ba 10\n        return updatedText.count <= 10\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u6aa2\u67e5\u4e86\u6587\u672c\u6846\u7684\u7576\u524d\u6587\u672c\uff0c\u4ee5\u53ca\u7528\u6236\u6253\u7b97\u63d2\u5165\u7684\u65b0\u6587\u672c\uff0c\u78ba\u4fdd\u8f38\u5165\u7684\u7e3d\u9577\u5ea6\u4e0d\u8d85\u904e 10 \u500b\u5b57\u7b26\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u6587\u672c\u4e0d\u986f\u793a\uff1a<\/strong>\u78ba\u4fdd\u5df2\u5c07\u6587\u672c\u6846\u7684 <code>delegate<\/code> \u8a2d\u7f6e\u70ba\u7576\u524d\u63a7\u5236\u5668\u3002<\/li>\n<li><strong>\u8f38\u5165\u904e\u9577\uff1a<\/strong>\u6aa2\u67e5 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u7684\u908f\u8f2f\uff0c\u78ba\u4fdd\u6b63\u78ba\u8655\u7406\u5b57\u7b26\u6578\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u9084\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u5728\u6587\u672c\u6846\u4e0b\u65b9\u986f\u793a\u7576\u524d\u5b57\u7b26\u6578\u3002<\/li>\n<li>\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u9032\u884c\u66f4\u5fa9\u96dc\u7684\u8f38\u5165\u9a57\u8b49\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u6587\u672c\u6846-\u6dfb\u52a0\u9650\u5236\u6587\u672c\u9577\u5ea6\u7684\u6587\u672c\u6846-\ud83d\udcdd_1675474410.098607.webp\" alt=\"Swift \u6587\u672c\u6846 - \u6dfb\u52a0\u9650\u5236\u6587\u672c\u9577\u5ea6\u7684\u6587\u672c\u6846 \ud83d\udcdd\" title=\"Swift\u6587\u672c\u6846\u3001\u9650\u5236\u6587\u672c\u9577\u5ea6\u3001\u6587\u672c\u6846\u8f38\u5165\u3001Swift\u7a0b\u5f0f\u8a9e\u6cd5\u3001\u6587\u672c\u6846\u529f\u80fd\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&A)<\/h2>\n<h3>1. \u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 UITextField \u9032\u884c\u5b57\u7b26\u9650\u5236\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u7528\u6236\u8f38\u5165\uff0c\u78ba\u4fdd\u4e0d\u8d85\u904e\u8a2d\u5b9a\u7684\u6700\u5927\u5b57\u7b26\u6578\u3002<\/p>\n<h3>2. UITextField \u7684\u6700\u5927\u9577\u5ea6\u53ef\u4ee5\u52d5\u614b\u6539\u8b8a\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u5728\u7a0b\u5f0f\u4e2d\u52d5\u614b\u8abf\u6574\u6700\u5927\u9577\u5ea6\uff0c\u4e26\u5728 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u4e2d\u9032\u884c\u76f8\u61c9\u7684\u6aa2\u67e5\u3002<\/p>\n<h3>3. \u662f\u5426\u53ef\u4ee5\u5728\u6587\u672c\u6846\u4e2d\u986f\u793a\u7576\u524d\u5b57\u7b26\u6578\uff1f<\/h3>\n<p>\u53ef\u4ee5\uff0c\u60a8\u53ef\u4ee5\u5728\u6587\u672c\u6846\u4e0b\u65b9\u6dfb\u52a0\u4e00\u500b\u6a19\u7c64\uff0c\u4e26\u5728 <code>shouldChangeCharactersIn<\/code> \u65b9\u6cd5\u4e2d\u66f4\u65b0\u8a72\u6a19\u7c64\u7684\u6587\u672c\uff0c\u4ee5\u986f\u793a\u7576\u524d\u8f38\u5165\u7684\u5b57\u7b26\u6578\u3002<\/p>\n<p>```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528\u6587\u672c\u6846\uff0c\u4e26\u9650\u5236\u6587\u672c\u6846\u7684\u6587\u672c\u9577\u5ea6\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u6587\u672c\u6846\u7684\u8f38\u5165\u3002<\/p>\n","protected":false},"author":1,"featured_media":7333,"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-7334","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-\u6587\u672c\u6846-\u6dfb\u52a0\u9650\u5236\u6587\u672c\u9577\u5ea6\u7684\u6587\u672c\u6846-\ud83d\udcdd_1675474410.098607.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ui","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7334","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=7334"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7333"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}