{"id":7402,"date":"2025-05-31T12:42:07","date_gmt":"2025-06-05T05:31:07","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7402"},"modified":"2025-06-05T12:42:07","modified_gmt":"2025-06-05T05:31:07","slug":"https-badgameshow-com-steven-372","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-372\/","title":{"rendered":"Swift UITextField \u6587\u5b57\u8f38\u5165\u6846\u6559\u5b78 &#8211; 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField, \u6587\u5b57\u8f38\u5165\u6846, iOS\u958b\u767c, \u8f38\u5165\u9650\u5236\"><\/p>\n<h2>Swift UITextField \u6587\u5b57\u8f38\u5165\u6846\u4ecb\u7d39<\/h2>\n<p>Swift UITextField\u662fiOS\u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5143\u4ef6\uff0c\u63d0\u4f9b\u4f7f\u7528\u8005\u8f38\u5165\u6587\u5b57\u7684\u529f\u80fd\u3002\u5b83\u4e0d\u50c5\u53ef\u4ee5\u63a5\u53d7\u6587\u5b57\u8f38\u5165\uff0c\u9084\u53ef\u4ee5\u8a2d\u7f6e\u8f38\u5165\u9650\u5236\uff0c\u4f8b\u5982\u9650\u5236\u53ea\u80fd\u8f38\u5165\u6578\u5b57\u6216\u7279\u5b9a\u5b57\u5143\u3002\u672c\u7bc7\u6587\u7ae0\u5c07\u5e36\u60a8\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528UITextField\uff0c\u4e26\u8a2d\u7f6e\u8f38\u5165\u9650\u5236\uff0c\u4ee5\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>\u5728Swift\u4e2d\u4f7f\u7528UITextField\u6587\u5b57\u8f38\u5165\u6846<\/h2>\n<p>\u5728Swift\u4e2d\u4f7f\u7528UITextField\uff0c\u60a8\u9996\u5148\u9700\u8981\u5728Storyboard\u4e2d\u62d6\u62c9\u4e00\u500bUITextField\u5230ViewController\u4e2d\uff0c\u4e26\u5c07\u5176\u985e\u5225\u8a2d\u7f6e\u70baUITextField\u3002\u4ee5\u4e0b\u662f\u5275\u5efaUITextField\u7684\u793a\u7bc4\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/fVVXv2T.png\" alt=\"UITextField\" title=\"\u5728Storyboard\u4e2d\u8a2d\u7f6eUITextField\" \/><\/p>\n<pre><code class=\"language-swift line-numbers\">let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 40))\ntextField.placeholder = \"\u8acb\u8f38\u5165\u6587\u5b57\"\ntextField.borderStyle = .roundedRect\ntextField.clearButtonMode = .whileEditing\ntextField.returnKeyType = .done\ntextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)\nview.addSubview(textField)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u5c07\u5275\u5efa\u4e00\u500bUITextField\uff0c\u4e26\u8a2d\u7f6e\u5176\u4f4d\u7f6e\u3001\u5927\u5c0f\u3001\u4f54\u4f4d\u6587\u5b57\u3001\u908a\u6846\u6a23\u5f0f\u3001\u6e05\u9664\u6309\u9215\u6a23\u5f0f\u53ca\u8fd4\u56de\u9375\u985e\u578b\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u8abf\u6574\u9019\u4e9b\u5c6c\u6027\u3002<\/p>\n<h2>\u8a2d\u5b9a\u6587\u5b57\u8f38\u5165\u7684\u9650\u5236<\/h2>\n<p>\u5728Swift\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>textField(_:shouldChangeCharactersIn:replacementString:)<\/code>\u65b9\u6cd5\u4f86\u9650\u5236\u8f38\u5165\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u53ea\u5141\u8a31\u6578\u5b57\u8f38\u5165\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {\n    \/\/ \u5141\u8a31\u7684\u5b57\u5143\u96c6\u70ba\u6578\u5b57\n    let allowedCharacters = CharacterSet.decimalDigits\n    let characterSet = CharacterSet(charactersIn: string)\n    return allowedCharacters.isSuperset(of: characterSet)\n}\n<\/code><\/pre>\n<p>\u6b64\u7a0b\u5f0f\u78bc\u6703\u6aa2\u67e5\u4f7f\u7528\u8005\u8f38\u5165\u7684\u5b57\u5143\u662f\u5426\u70ba\u6578\u5b57\uff0c\u82e5\u662f\u5247\u8fd4\u56de<code>true<\/code>\uff0c\u5426\u5247\u8fd4\u56de<code>false<\/code>\uff0c\u4ee5\u6b64\u6c7a\u5b9a\u662f\u5426\u5141\u8a31\u8f38\u5165\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fddUITextField\u7684delegate\u5df2\u8a2d\u7f6e\uff0c\u5426\u5247<code>textField(_:shouldChangeCharactersIn:replacementString:)<\/code>\u65b9\u6cd5\u5c07\u4e0d\u88ab\u547c\u53eb\u3002<\/li>\n<li>\u5728\u4f7f\u7528UITextField\u6642\uff0c\u8acb\u6ce8\u610fUI Thread\u7684\u64cd\u4f5c\uff0c\u78ba\u4fdd\u4e0d\u6703\u9020\u6210UI\u5361\u9813\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u6587\u5b57\u8f38\u5165\uff0cUITextField\u9084\u53ef\u4ee5\u8207\u5176\u4ed6UI\u5143\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u6bd4\u5982:<\/p>\n<ul>\n<li>\u8207UIButton\u7d50\u5408\uff0c\u5be6\u73fe\u6309\u9215\u9ede\u64ca\u5f8c\u6aa2\u67e5\u8f38\u5165\u5167\u5bb9\u3002<\/li>\n<li>\u8207UILabel\u7d50\u5408\uff0c\u5be6\u73fe\u5373\u6642\u986f\u793a\u8f38\u5165\u7d50\u679c\u3002<\/li>\n<\/ul>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528UITextField\u6587\u5b57\u8f38\u5165\u6846\uff0c\u4ee5\u53ca\u8a2d\u5b9a\u6587\u5b57\u8f38\u5165\u7684\u9650\u5236\u3002\u900f\u904eUITextField\u7684\u4f7f\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u8655\u7406\u4f7f\u7528\u8005\u7684\u8f38\u5165\uff0c\u63d0\u5347\u6574\u9ad4\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UITextField\u5982\u4f55\u8a2d\u7f6e\u908a\u6846\u6a23\u5f0f\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>borderStyle<\/code>\u5c6c\u6027\u4f86\u8a2d\u7f6eUITextField\u7684\u908a\u6846\u6a23\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528<code>.roundedRect<\/code>\u4f86\u7372\u5f97\u5713\u89d2\u908a\u6846\u6548\u679c\u3002<\/p>\n<h3>2. UITextField\u652f\u6301\u54ea\u4e9b\u8f38\u5165\u683c\u5f0f\uff1f<\/h3>\n<p>UITextField\u652f\u6301\u591a\u7a2e\u8f38\u5165\u683c\u5f0f\uff0c\u901a\u904e\u8a2d\u7f6e<code>keyboardType<\/code>\u5c6c\u6027\uff0c\u53ef\u4ee5\u9078\u64c7\u6578\u5b57\u9375\u76e4\u3001\u96fb\u5b50\u90f5\u4ef6\u9375\u76e4\u7b49\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728UITextField\u4e2d\u8a2d\u7f6e\u4f54\u4f4d\u6587\u5b57\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>placeholder<\/code>\u5c6c\u6027\u4f86\u8a2d\u7f6eUITextField\u7684\u4f54\u4f4d\u6587\u5b57\uff0c\u4f8b\u5982<code>textField.placeholder = \"\u8acb\u8f38\u5165\u6587\u5b57\"<\/code>\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39SwiftUITextField\u6587\u5b57\u8f38\u5165\u6846\u7684\u57fa\u672c\u529f\u80fd\uff0c\u5305\u62ec\u5982\u4f55\u8a2d\u7f6e\u6587\u5b57\u8f38\u5165\u6846\u7684\u5c6c\u6027\uff0c\u5982\u4f55\u8a2d\u7f6e\u6587\u5b57\u8f38\u5165\u6846\u7684\u6a23\u5f0f\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u6587\u5b57\u8f38\u5165\u6846\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":7400,"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-7402","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\u6587\u5b57\u8f38\u5165\u6846_1675475295.082096.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Vo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7402","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=7402"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7402\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7400"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}