{"id":7688,"date":"2025-05-29T14:09:24","date_gmt":"2025-06-05T06:50:23","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7688"},"modified":"2025-06-05T14:09:24","modified_gmt":"2025-06-05T06:50:23","slug":"https-badgameshow-com-steven-487","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-487\/","title":{"rendered":"\u5728 SwiftUI \u4e2d\u9ad8\u6548\u4f7f\u7528 Text \u548c Image\uff1a2025 \u6700\u65b0\u6559\u5b78\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, SwiftUI, Text, Image, SwiftUI \u6559\u5b78, SwiftUI \u6700\u65b0\u8a9e\u6cd5\"><\/p>\n<h2>\u6982\u8ff0<\/h2>\n<p>\u5728 SwiftUI \u4e2d\u4f7f\u7528 Text \u548c Image \u662f\u4e00\u500b\u57fa\u672c\u800c\u5e38\u898b\u7684\u9700\u6c42\uff0c\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u8f15\u9b06\u5730\u5c55\u793a\u6587\u5b57\u548c\u5716\u7247\u3002\u96a8\u8457 SwiftUI \u7684\u4e0d\u65b7\u66f4\u65b0\uff0c\u672c\u6587\u5c07\u4ecb\u7d39 **2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10**\uff0c\u4e26\u63d0\u4f9b\u8a73\u7d30\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u4f60\u6253\u9020\u66f4\u7f8e\u89c0\u7684 App\u3002<\/p>\n<h2>\u4f7f\u7528 Text<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u4f7f\u7528 <code>Text<\/code> \u4f86\u986f\u793a\u6587\u5b57\u975e\u5e38\u7c21\u55ae\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u7528\u4e00\u500b <code>String<\/code> \u4f5c\u70ba\u53c3\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">Text(\"Hello, World!\")\n<\/code><\/pre>\n<p>\u9664\u4e86\u63a5\u53d7 <code>String<\/code>\uff0c<code>Text<\/code> \u9084\u80fd\u63a5\u53d7\u4e00\u500b <code>AttributedString<\/code> \u4f5c\u70ba\u53c3\u6578\uff0c\u9019\u6a23\u4f60\u53ef\u4ee5\u9748\u6d3b\u5730\u6539\u8b8a\u6587\u5b57\u7684\u984f\u8272\u3001\u5927\u5c0f\u548c\u5b57\u578b\u7b49\u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let attributedString = AttributedString(\"Hello, World!\", attributes: .init([\n    .font: UIFont.systemFont(ofSize: 20),\n    .foregroundColor: UIColor.red\n]))\nText(attributedString)\n<\/code><\/pre>\n<h2>\u4f7f\u7528 Image<\/h2>\n<p>\u5728 SwiftUI \u4e2d\uff0c\u4f7f\u7528 <code>Image<\/code> \u4f86\u986f\u793a\u5716\u7247\u4e5f\u5f88\u7c21\u55ae\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>UIImage<\/code> \u4f86\u8f09\u5165\u672c\u5730\u5716\u7247\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let image = UIImage(named: \"myImage\") {\n    Image(uiImage: image)\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5f9e\u7db2\u8def\u4e0a\u8f09\u5165\u5716\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>AsyncImage<\/code>\uff0c\u9019\u662f SwiftUI \u4e2d\u7684\u4e00\u500b\u65b0\u7279\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">AsyncImage(url: URL(string: \"https:\/\/example.com\/myImage.png\")) { image in\n    image.resizable().scaledToFit()\n} placeholder: {\n    ProgressView()\n}\n<\/code><\/pre>\n<h2>\u7d50\u5408 Text \u548c Image<\/h2>\n<p>\u70ba\u4e86\u66f4\u597d\u5730\u5c55\u793a\u6587\u5b57\u548c\u5716\u7247\uff0c\u4f60\u53ef\u4ee5\u5c07 <code>Text<\/code> \u548c <code>Image<\/code> \u7d50\u5408\u5728\u4e00\u8d77\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>HStack<\/code> \u4f86\u6c34\u5e73\u6392\u5217\u5b83\u5011\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">HStack {\n    Image(systemName: \"star.fill\") \/\/ \u4f7f\u7528 SF Symbols \u793a\u4f8b\n        .resizable()\n        .frame(width: 20, height: 20)\n    Text(\"Hello, World!\")\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u5728 App \u4e2d\u540c\u6642\u986f\u793a\u6587\u5b57\u548c\u5716\u7247\uff0c\u8b93\u4f60\u7684 App \u66f4\u52a0\u7f8e\u89c0\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 <code>Text<\/code> \u548c <code>Image<\/code> \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff1a<br \/>\n&#8211; **\u5716\u7247\u7121\u6cd5\u986f\u793a**\uff1a\u78ba\u8a8d\u5716\u7247\u540d\u7a31\u662f\u5426\u6b63\u78ba\uff0c\u6216\u8005\u6aa2\u67e5 URL \u662f\u5426\u6709\u6548\u3002<br \/>\n&#8211; **\u6587\u5b57\u683c\u5f0f\u4e0d\u6b63\u78ba**\uff1a\u78ba\u4fdd\u4f7f\u7528\u6b63\u78ba\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u4f86\u8a2d\u7f6e <code>AttributedString<\/code>\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u4f7f\u7528 <code>Image<\/code> \u548c <code>Text<\/code> \u4f86\u88fd\u4f5c\u5361\u7247\u6a23\u5f0f\u7684 UI\u3002<br \/>\n&#8211; \u5229\u7528 <code>HStack<\/code> \u548c <code>VStack<\/code> \u7d44\u5408\u4e0d\u540c\u7684\u8996\u5716\uff0c\u5275\u5efa\u66f4\u8907\u96dc\u7684\u754c\u9762\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528-Text-\u548c-Image_1676959980.880373.webp\" alt=\"\u5728 SwiftUI \u4e2d\u4f7f\u7528 Text \u548c Image\" title=\"SwiftUI\u3001Text\u3001Image\u3001\u4ecb\u9762\u3001\u7bc4\u4f8b\" 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 SwiftUI \u4e2d\u81ea\u5b9a\u7fa9\u6587\u5b57\u6a23\u5f0f\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>AttributedString<\/code> \u4f86\u8a2d\u7f6e\u5b57\u9ad4\u3001\u984f\u8272\u7b49\u5c6c\u6027\uff0c\u5177\u9ad4\u65b9\u6cd5\u53ef\u4ee5\u53c3\u8003\u4e0a\u8ff0\u7684\u793a\u4f8b\u3002<\/p>\n<h3>2. \u5982\u4f55\u5f9e\u7db2\u8def\u52a0\u8f09\u5716\u7247\uff1f<\/h3>\n<p>\u4f7f\u7528 <code>AsyncImage<\/code> \u53ef\u4ee5\u7c21\u55ae\u5730\u5f9e URL \u52a0\u8f09\u5716\u7247\uff0c\u4e26\u4e14\u652f\u6301\u986f\u793a\u52a0\u8f09\u4f54\u4f4d\u7b26\u3002<\/p>\n<h3>3. SwiftUI \u4e2d\u7684\u5716\u7247\u548c\u6587\u5b57\u662f\u5426\u652f\u6301\u52d5\u614b\u66f4\u65b0\uff1f<\/h3>\n<p>\u662f\u7684\uff0cSwiftUI \u652f\u6301\u52d5\u614b\u66f4\u65b0\uff0c\u53ea\u9700\u78ba\u4fdd\u4f60\u7684\u6578\u64da\u6e90\u662f\u53ef\u89c0\u5bdf\u7684\uff08\u5982\u4f7f\u7528 <code>@State<\/code> \u6216 <code>@Binding<\/code>\uff09\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728SwiftUI\u4e2d\u4f7f\u7528Text\u548cImage\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728SwiftUI\u4e2d\u5efa\u7acb\u51fa\u6f02\u4eae\u7684\u4ecb\u9762\u3002\u6211\u5011\u5c07\u6703\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Text\u548cImage\u4f86\u5efa\u7acb\u51fa\u7f8e\u89c0\u7684\u4ecb\u9762\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5feb\u5730\u4e0a\u624b\u3002<\/p>\n","protected":false},"author":1,"featured_media":7687,"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-7688","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\/\u5728-SwiftUI-\u4e2d\u4f7f\u7528-Text-\u548c-Image_1676959980.880373.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-200","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7688","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=7688"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7688\/revisions"}],"predecessor-version":[{"id":13420,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7688\/revisions\/13420"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7687"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}