{"id":6991,"date":"2025-05-30T14:45:49","date_gmt":"2025-06-05T06:56:49","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6991"},"modified":"2025-06-05T14:45:49","modified_gmt":"2025-06-05T06:56:49","slug":"https-badgameshow-com-steven-171","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-171\/","title":{"rendered":"Swift UITextView \u81ea\u8a02\u5b57\u9ad4\u6559\u5b78 2025 \u6700\u65b0\u5be6\u8e10 \ud83c\udfa8"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextView, \u81ea\u5b9a\u7fa9\u5b57\u9ad4, iOS \u958b\u767c, \u5b57\u9ad4\u8a2d\u5b9a, Swift \u6559\u5b78\"><\/p>\n<h2>\u7c21\u4ecb<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5229\u7528\u5b83\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684 iOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728 Swift \u4e2d\uff0c\u4f7f\u7528 <code>UITextView<\/code> \u4f86\u81ea\u5b9a\u7fa9\u5b57\u9ad4\u53ef\u4ee5\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u53ef\u8996\u6027\u548c\u53ef\u7528\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 2025 \u5e74\u4f7f\u7528 Swift \u7684\u6700\u65b0\u8a9e\u6cd5\u4f86\u81ea\u5b9a\u7fa9 <code>UITextView<\/code> \u7684\u5b57\u9ad4\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5275\u5efa UITextView<\/h2>\n<p>\u9996\u5148\uff0c\u958b\u767c\u8005\u9700\u8981\u5728 Xcode \u4e2d\u5275\u5efa\u4e00\u500b\u65b0\u7684 Swift \u5c08\u6848\uff0c\u4e26\u5728\u756b\u9762\u4e0a\u6dfb\u52a0\u4e00\u500b <code>UITextView<\/code> \u63a7\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u521d\u59cb\u5316\u4e26\u914d\u7f6e <code>UITextView<\/code> \u7684\u57fa\u672c\u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let textView = UITextView()\ntextView.frame = CGRect(x: 20, y: 50, width: 300, height: 200)\ntextView.backgroundColor = UIColor.lightGray\ntextView.textColor = UIColor.black\ntextView.font = UIFont(name: \"HelveticaNeue-Bold\", size: 18)\ntextView.text = \"\u9019\u662f\u81ea\u5b9a\u7fa9\u5b57\u9ad4\u7684 UITextView\"\n<\/code><\/pre>\n<h2>\u81ea\u5b9a\u7fa9\u5b57\u9ad4<\/h2>\n<p>\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u81ea\u5b9a\u7fa9 <code>UITextView<\/code> \u7684\u5b57\u9ad4\uff0c\u4f8b\u5982\u66f4\u6539\u5b57\u9ad4\u5927\u5c0f\u548c\u984f\u8272\u3002\u4ee5\u4e0b\u662f\u66f4\u6539\u5b57\u9ad4\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">textView.font = UIFont.systemFont(ofSize: 20) \/\/ \u66f4\u6539\u5b57\u9ad4\u5927\u5c0f\ntextView.textColor = UIColor.blue \/\/ \u66f4\u6539\u5b57\u9ad4\u984f\u8272\n<\/code><\/pre>\n<h2>\u4f7f\u7528 AttributedString \u9032\u884c\u9032\u968e\u5b57\u9ad4\u8a2d\u5b9a<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5b57\u9ad4\u8a2d\u5b9a\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u4f7f\u7528 <code>attributedText<\/code> \u5c6c\u6027\u4f86\u9032\u884c\u66f4\u7d30\u7dfb\u7684\u5b57\u9ad4\u63a7\u5236\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let attributedString = NSMutableAttributedString(string: \"Hello World!\")\nattributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 5)) \/\/ \u8a2d\u5b9a\u524d\u4e94\u500b\u5b57\u70ba\u7d05\u8272\ntextView.attributedText = attributedString\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u914d\u7f6e <code>UITextView<\/code> \u7684\u5b57\u9ad4\u6642\uff0c\u82e5\u767c\u751f\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\uff1a<\/p>\n<ul>\n<li><strong>\u5b57\u9ad4\u540d\u7a31\u932f\u8aa4\uff1a<\/strong>\u78ba\u4fdd\u5b57\u9ad4\u540d\u7a31\u6b63\u78ba\u4e26\u5df2\u5305\u542b\u5728\u5c08\u6848\u4e2d\u3002<\/li>\n<li><strong>\u5b57\u9ad4\u5927\u5c0f\u70ba\u96f6\uff1a<\/strong>\u78ba\u8a8d\u5b57\u9ad4\u5927\u5c0f\u8a2d\u5b9a\u70ba\u5927\u65bc\u96f6\u7684\u503c\u3002<\/li>\n<li><strong>\u6587\u5b57\u672a\u986f\u793a\uff1a<\/strong>\u6aa2\u67e5 <code>UITextView<\/code> \u7684\u6846\u67b6\u8a2d\u5b9a\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f7f\u7528 <code>UITextView<\/code> \u6642\uff0c\u4e5f\u53ef\u4ee5\u8003\u616e\u5c07\u5176\u8207\u5176\u4ed6 UI \u5143\u4ef6\u7d50\u5408\uff0c\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u7d50\u5408 <code>UILabel<\/code> \u986f\u793a\u6a19\u984c\u548c\u8aaa\u660e\u3002<\/li>\n<li>\u4f7f\u7528 <code>UIScrollView<\/code> \u9032\u884c\u5167\u5bb9\u7684\u6ed1\u52d5\u986f\u793a\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u4e2d\u7684 <code>UITextView<\/code> \u8b93\u958b\u767c\u8005\u80fd\u5920\u9748\u6d3b\u5730\u81ea\u5b9a\u7fa9\u5b57\u9ad4\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6574\u9ad4\u8cea\u611f\u3002\u900f\u904e <code>font<\/code> \u548c <code>attributedText<\/code> \u5c6c\u6027\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u8abf\u6574\u5b57\u9ad4\u6a23\u5f0f\u4f86\u7b26\u5408\u61c9\u7528\u9700\u6c42\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITextView\u81ea\u5b9a\u7fa9\u5b57\u9ad4-\ud83c\udfa8_1675400042.911467.webp\" alt=\"Swift UITextView\u81ea\u5b9a\u7fa9\u5b57\u9ad4 \ud83c\udfa8\" title=\"Swift UITextView\u81ea\u5b9a\u7fa9\u5b57\u9ad4\u3001Swift UITextView\u5b57\u9ad4\u8a2d\u5b9a\u3001Swift UITextView\u5b57\u9ad4\u66f4\u6539\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 UITextView \u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u5b57\u9ad4\u6a23\u5f0f\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>attributedText<\/code> \u5c6c\u6027\u70ba\u4e0d\u540c\u5b57\u5143\u6216\u5b57\u4e32\u8a2d\u5b9a\u4e0d\u540c\u7684\u5b57\u9ad4\u6a23\u5f0f\u548c\u984f\u8272\u3002<\/p>\n<h3>2. UITextView \u662f\u5426\u652f\u6301\u5bcc\u6587\u672c\u683c\u5f0f\uff1f<\/h3>\n<p>\u662f\u7684\uff0c<code>UITextView<\/code> \u652f\u6301\u5bcc\u6587\u672c\u683c\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <code>NSMutableAttributedString<\/code> \u9032\u884c\u5b57\u9ad4\u6a23\u5f0f\u7684\u81ea\u8a02\u3002<\/p>\n<h3>3. \u5982\u4f55\u89e3\u6c7a\u5b57\u9ad4\u4e0d\u986f\u793a\u7684\u554f\u984c\uff1f<\/h3>\n<p>\u8acb\u6aa2\u67e5\u5b57\u9ad4\u540d\u7a31\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u5b57\u9ad4\u5df2\u5305\u62ec\u5728\u5c08\u6848\u4e2d\uff0c\u53e6\u5916\u78ba\u8a8d\u5b57\u9ad4\u5927\u5c0f\u662f\u5426\u8a2d\u7f6e\u70ba\u6709\u6548\u503c\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Swift\u4e2d\u81ea\u5b9a\u7fa9UITextView\u5b57\u9ad4\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u66f4\u6539\u6587\u5b57\u7684\u5916\u89c0\uff0c\u4e26\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u770b\u8d77\u4f86\u66f4\u52a0\u7368\u7279\u3002<\/p>\n","protected":false},"author":1,"featured_media":6989,"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-6991","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-UITextView\u81ea\u5b9a\u7fa9\u5b57\u9ad4-\ud83c\udfa8_1675400042.911467.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1OL","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6991","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=6991"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6991\/revisions"}],"predecessor-version":[{"id":13365,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6991\/revisions\/13365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6989"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}