{"id":310415,"date":"2021-01-14T11:44:55","date_gmt":"2021-01-14T03:44:55","guid":{"rendered":"http:\/\/4563.org\/?p=310415"},"modified":"2021-01-14T11:44:55","modified_gmt":"2021-01-14T03:44:55","slug":"%e4%b8%80%e4%b8%aa-swift-%e7%bc%96%e5%86%99%ef%bc%8c%e8%bd%bb%e9%87%8f%e7%ae%80%e6%b4%81%e7%9a%84-ios-%e4%b8%bb%e9%a2%98%e7%ae%a1%e7%90%86%e5%ba%93","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=310415","title":{"rendered":"\u4e00\u4e2a Swift \u7f16\u5199\uff0c\u8f7b\u91cf\u7b80\u6d01\u7684 iOS \u4e3b\u9898\u7ba1\u7406\u5e93"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u4e2a Swift \u7f16\u5199\uff0c\u8f7b\u91cf\u7b80\u6d01\u7684 iOS \u4e3b\u9898\u7ba1\u7406\u5e93               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Yunhao <\/span>  <span><i><\/i> 11<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u82f9\u679c\u5728 iOS 13 \u63a8\u51fa\u4e86 Dark Mode\uff0c\u53ef\u4ee5\u8ba9 iOS App \u62e5\u6709 Light \u548c Dark \u4e24\u5957\u4e3b\u9898\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u7684 App \u9700\u8981\u652f\u6301\u66f4\u591a\u4e3b\u9898\u5207\u6362\uff0c\u53ef\u4ee5\u8bd5\u4e00\u8bd5 Crystal\uff0c\u4e00\u4e2a\u8f7b\u91cf\u3001\u7b80\u6d01\u7684 iOS \u4e3b\u9898\u7ba1\u7406\u5e93\u3002<\/p>\n<p>https:\/\/github.com\/yunhao\/Crystal<\/p>\n<p>\u5982\u679c\u4f60\u5bf9\u8fd9\u4e2a\u5e93\u611f\u5174\u8da3\uff0c\u6b22\u8fce Star \u3002<\/p>\n<h3>\u5b9a\u4e49\u4e3b\u9898\uff0c\u7c7b\u578b\u5b89\u5168<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 class\uff0cstruct \u751a\u81f3 enum \u6765\u5b9a\u4e49\u4f60\u7684 Theme\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u9879\u76ee\u7684\u67d0\u4e2a\u5730\u65b9\u96c6\u4e2d\u7ba1\u7406\u8fd9\u4e9b\u4e3b\u9898\uff1a<\/p>\n<pre><code>\/\/ \u5b9a\u4e49\u4e3b\u9898\u7c7b\u578b\u3002 public struct AppTheme {     var textColor: UIColor     var backgroundColor: UIColor }  \/\/ \u5b9e\u4f8b\u5316\u4e3b\u9898\uff0c\u4e00\u4e2a\u4e3b\u9898\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002 extension AppTheme {     static var light: AppTheme {         return AppTheme(textColor: .black, backgroundColor: .white)     }      static var sunny: AppTheme {         return AppTheme(textColor: .yellow, backgroundColor: .white)     }     \/\/ ... \u66f4\u591a\u4e3b\u9898 }  extension AppTheme: CrystalThemeType {     \/\/ \u8fd4\u56de app \u542f\u52a8\u65f6\u7684\u521d\u59cb\u4e3b\u9898\u3002     public static var entry: CrystalThemeType { Self.light } } <\/code><\/pre>\n<p>\u4e3b\u9898\u4e0d\u9700\u8981 Hard-coding \u3002\u7ed3\u5408 Swift Codable \u534f\u8bae\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u3001\u4ece\u7f51\u7edc\u8bf7\u6c42 JSON\uff0c\u7136\u540e\u52a8\u6001\u52a0\u8f7d\u4e3b\u9898\u3002<\/p>\n<h3>\u8bbe\u7f6e\u4e3b\u9898\uff0c\u7b80\u6d01<\/h3>\n<p>\u6240\u6709\u7531\u5e93\u5f15\u5165\u7684\u6269\u5c55\u65b9\u6cd5\u90fd\u66b4\u9732\u5728\u76ee\u6807\u5bf9\u8c61\u7684 <code>.cst<\/code> \u5c5e\u6027\u4e0b\uff0c\u65e0\u5176\u4ed6\u989d\u5916\u5c5e\u6027\u5f15\u5165\uff0c\u4e0d\u4f1a\u6c61\u67d3\u76ee\u6807\u5bf9\u8c61\u3002<\/p>\n<p>\u4f60\u53ea\u9700\u8981\u628a\u4e3b\u9898\u7684\u503c\u8d4b\u4e88\u76ee\u6807\u5bf9\u8c61\u4e0a\uff0c\u5c31\u50cf\u5728 UIKit \u4e2d\u7ecf\u5e38\u505a\u7684\u90a3\u6837\u3002\u5373\u4f7f\u4e4b\u540e\u589e\u52a0\u3001\u5220\u9664\u4e3b\u9898\uff0c\u8fd9\u90e8\u5206\u4ee3\u7801\u4e5f\u4e0d\u9700\u8981\u4fee\u6539\uff01<\/p>\n<pre><code>\/\/ \u4e3a\u6309\u94ae\u8bbe\u7f6e\u4e3b\u9898\u3002 doneButton.cst.apply { button, theme in     button.setTitleColor(theme.textColor, for: .normal) } \/\/ \u81ea\u5b9a\u4e49 view \u3002 cardView.cst.apply { card, theme in      card.backgroundColor = theme.backgroundColor     card.someTextColor = theme.textColor } \/\/ Swift \u7b80\u5199\u3002 imageView.cst.apply { $0.tintColor = $1.textColor } <\/code><\/pre>\n<h3>\u5207\u6362\u4e3b\u9898<\/h3>\n<pre><code>\/\/ \u5207\u6362\u4e3b\u9898 Crystal.shared.theme = .sunny  \/\/ \u6216\u8005\uff0c\u52a0\u4e00\u4e2a\u52a8\u753b UIView.animate {   Crystal.shared.theme = .light } <\/code><\/pre>\n<h3>\u4fee\u6539\u4e3b\u9898\uff0c\u8f7b\u677e\u7ef4\u62a4<\/h3>\n<pre><code>\/\/ \u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u4e3b\u9898 extension AppTheme {     static var summer: AppTheme {         return AppTheme(textColor: .red, backgroundColor: .white)     } }  Crystal.shared.theme = .summer  \/\/ \u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u4e3b\u9898\u3002 Crystal.shared.theme = AppTheme(textColor: .red, backgroundColor: .white) <\/code><\/pre>\n<p>\u4e3a\u4e86\u4e0d\u8fc7\u4e8e\u5197\u957f\uff0c\u8fd9\u91cc\u7701\u7565\u4e86\u90e8\u5206\u4ee3\u7801\uff0c\u6b22\u8fce\u5230\u9879\u76ee\u4e3b\u9875\u67e5\u770b README \u3002<\/p>\n<p>Crystal \u4f7f\u7528 Weak Key Dictionary \u6765\u7ba1\u7406\u8fd9\u4e9b\u5bf9\u8c61\u548c\u95ed\u5305\uff0c\u5728\u5fc5\u8981\u65f6\u81ea\u52a8\u91ca\u653e\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\uff0c\u5177\u4f53\u5b9e\u73b0\u53ef\u53c2\u8003\u6e90\u7801\u3002<\/p>\n<p>\u9879\u76ee\u5730\u5740\uff1a https:\/\/github.com\/yunhao\/Crystal<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4993921\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vincentxue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ee3\u7801\u5199\u7684\u4e0d\u9519\uff0c\u5982\u679c\u662f\u975e\u4fb5\u5165\u5f0f\u8bbe\u8ba1\u5c31\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4993922\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Yunhao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @vincentxue \u611f\u8c22\u3002\u7ee7\u7eed\u6df1\u5165\u5b66\u4e60 Swift\uff0c\u770b\u80fd\u4e0d\u80fd\u5199\u7684\u518d\u597d\u4e00\u4e9b\uff5e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a Swift \u7f16\u5199\uff0c\u8f7b\u91cf\u7b80\u6d01\u7684&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/310415"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=310415"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/310415\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}