{"id":7434,"date":"2025-05-31T13:49:24","date_gmt":"2025-06-05T06:31:23","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7434"},"modified":"2025-06-05T13:49:24","modified_gmt":"2025-06-05T06:31:23","slug":"https-badgameshow-com-steven-388","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-388\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u8868\u55ae\u88fd\u4f5c\u6559\u5b78\uff1a\u4f7f\u7528 UITextField \u548c UILabel"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField, UILabel, Swift \u6559\u5b78, iOS \u958b\u767c\"><\/p>\n<h1>2025 \u6700\u65b0 Swift \u8868\u55ae\u88fd\u4f5c\u6559\u5b78\uff1a\u4f7f\u7528 UITextField \u548c UILabel<\/h1>\n<p>\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>UITextField<\/code> \u548c <code>UILabel<\/code> \u4f86\u88fd\u4f5c\u8868\u55ae\u3002<code>UITextField<\/code> \u5141\u8a31\u4f7f\u7528\u8005\u8f38\u5165\u6587\u5b57\uff0c\u800c <code>UILabel<\/code> \u5247\u7528\u4f86\u986f\u793a\u6587\u5b57\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u88fd\u4f5c\u8868\u55ae\uff0c\u4e26\u793a\u7bc4\u5982\u4f55\u61c9\u7528\u9019\u5169\u500b\u5143\u4ef6\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u900f\u904e\u5be6\u4f5c\u7bc4\u4f8b\u4f86\u6df1\u5165\u77ad\u89e3\u3002<\/p>\n<h2>\u6b65\u9a5f 1\uff1a\u5728 Storyboard \u4e2d\u65b0\u589e\u5143\u4ef6<\/h2>\n<p>\u9996\u5148\uff0c\u5728 Xcode \u7684 Storyboard \u4e2d\u65b0\u589e\u4e00\u500b <code>UITextField<\/code> \u548c\u4e00\u500b <code>UILabel<\/code>\u3002\u78ba\u4fdd\u5c07\u5b83\u5011\u64fa\u653e\u5728\u756b\u9762\u4e0a\u9069\u7576\u7684\u4f4d\u7f6e\uff0c\u4ee5\u4fbf\u4f7f\u7528\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u8f38\u5165\u548c\u67e5\u770b\u3002<\/p>\n<h2>\u6b65\u9a5f 2\uff1a\u8a2d\u5b9a UITextField \u548c UILabel<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u8a2d\u5b9a <code>UITextField<\/code> \u548c <code>UILabel<\/code> \u7684\u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u8a2d\u5b9a UITextField\ntextField.placeholder = \"\u8acb\u8f38\u5165\u6587\u5b57\"\ntextField.borderStyle = .roundedRect\n\n\/\/ \u8a2d\u5b9a UILabel\nlabel.text = \"\u986f\u793a\u6587\u5b57\"\nlabel.textColor = .black\nlabel.font = UIFont.systemFont(ofSize: 17)\n<\/code><\/pre>\n<h2>\u6b65\u9a5f 3\uff1a\u5c07 UITextField \u7684\u8f38\u5165\u986f\u793a\u5728 UILabel \u4e0a<\/h2>\n<p>\u6211\u5011\u5c07\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5c07 <code>UITextField<\/code> \u8f38\u5165\u7684\u6587\u5b57\u52d5\u614b\u986f\u793a\u5728 <code>UILabel<\/code> \u4e0a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5c07 UITextField \u8f38\u5165\u7684\u6587\u5b57\u986f\u793a\u5728 UILabel \u4e0a\ntextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)\n\n@objc func textFieldDidChange(_ textField: UITextField) {\n    label.text = textField.text\n}\n<\/code><\/pre>\n<h2>\u6b65\u9a5f 4\uff1a\u4f7f\u7528 Auto Layout \u653e\u7f6e\u5143\u4ef6<\/h2>\n<p>\u6700\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5c07 <code>UITextField<\/code> \u548c <code>UILabel<\/code> \u653e\u7f6e\u5728\u756b\u9762\u4e0a\uff0c\u78ba\u4fdd\u5b83\u5011\u7684\u6392\u7248\u7b26\u5408\u8a2d\u8a08\u9700\u6c42\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5c07 UITextField \u548c UILabel \u653e\u7f6e\u5728\u756b\u9762\u4e0a\ntextField.translatesAutoresizingMaskIntoConstraints = false\nlabel.translatesAutoresizingMaskIntoConstraints = false\n\nNSLayoutConstraint.activate([\n    textField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),\n    textField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),\n    textField.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),\n\n    label.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 16),\n    label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),\n    label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),\n])\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\u9047\u5230\u554f\u984c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\uff1a<br \/>\n&#8211; **UITextField \u7121\u6cd5\u986f\u793a\u6587\u5b57**\uff1a\u78ba\u4fdd\u5df2\u6b63\u78ba\u8a2d\u7f6e placeholder\uff0c\u4e26\u6aa2\u67e5 textFieldDidChange \u65b9\u6cd5\u662f\u5426\u5df2\u9023\u63a5\u6b63\u78ba\u3002<br \/>\n&#8211; **UILabel \u672a\u66f4\u65b0**\uff1a\u78ba\u8a8d\u662f\u5426\u5df2\u6b63\u78ba\u8a2d\u5b9a target\uff0c\u4e26\u6aa2\u67e5\u662f\u5426\u6709\u5176\u4ed6\u7a0b\u5f0f\u78bc\u5f71\u97ff UILabel \u7684\u986f\u793a\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u8868\u55ae\u88fd\u4f5c\uff0c\u60a8\u9084\u53ef\u4ee5\u64f4\u5c55\u9019\u500b\u7bc4\u4f8b\uff0c\u4f8b\u5982\u589e\u52a0\u6309\u9215\u4f86\u63d0\u4ea4\u8868\u55ae\uff0c\u6216\u8005\u5c07\u8f38\u5165\u7684\u6587\u5b57\u5132\u5b58\u5230\u8cc7\u6599\u5eab\u4e2d\u3002\u9019\u4e9b\u529f\u80fd\u53ef\u4ee5\u8b93\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u5177\u4e92\u52d5\u6027\u548c\u5be6\u7528\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8868\u55ae\u88fd\u4f5c-UITextField-UILabel_1675475872.98999.webp\" alt=\"Swift \u8868\u55ae\u88fd\u4f5c (UITextField &#038; UILabel)\" title=\"Swift \u8868\u55ae\u88fd\u4f5c, UITextField, UILabel, iOS \u8868\u55ae\u88fd\u4f5c, Swift \u8a9e\u8a00\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UITextField \u5982\u4f55\u9650\u5236\u5b57\u6578\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528 <code>delegate<\/code> \u65b9\u6cd5\u4f86\u9650\u5236\u5b57\u6578\uff0c\u76e3\u807d <code>textField(_:shouldChangeCharactersIn:replacementString:)<\/code> \u65b9\u6cd5\u4f86\u5be6\u73fe\u3002<\/p>\n<h3>Q2: UILabel \u5982\u4f55\u81ea\u52d5\u63db\u884c\uff1f<\/h3>\n<p>A2: \u8a2d\u5b9a <code>numberOfLines<\/code> \u5c6c\u6027\u70ba 0\uff0c\u4e26\u78ba\u4fdd <code>lineBreakMode<\/code> \u8a2d\u70ba <code>byWordWrapping<\/code>\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u88fd\u4f5cUITextField&#038;UILabel\uff0c\u5b78\u7fd2\u5982\u4f55\u5728iOS\u4e0a\u4f7f\u7528Swift\u8a9e\u8a00\u5efa\u7acb\u8868\u55ae\uff0c\u4ee5\u53ca\u5982\u4f55\u5c07UITextField&#038;UILabel\u52a0\u5165\u8868\u55ae\u4e2d\u3002<\/p>\n","protected":false},"author":1,"featured_media":7433,"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-7434","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-\u8868\u55ae\u88fd\u4f5c-UITextField-UILabel_1675475872.98999.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1VU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7434","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=7434"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7434\/revisions"}],"predecessor-version":[{"id":13518,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7434\/revisions\/13518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7433"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}