UIKit+Theme.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // UIKit+Theme.swift
  3. // SwiftTheme
  4. //
  5. // Created by Gesen on 16/1/22.
  6. // Copyright © 2016年 Gesen. All rights reserved.
  7. //
  8. import UIKit
  9. @objc public extension UIView
  10. {
  11. var theme_alpha: ThemeCGFloatPicker? {
  12. get { return getThemePicker(self, "setAlpha:") as? ThemeCGFloatPicker }
  13. set { setThemePicker(self, "setAlpha:", newValue) }
  14. }
  15. var theme_backgroundColor: ThemeColorPicker? {
  16. get { return getThemePicker(self, "setBackgroundColor:") as? ThemeColorPicker }
  17. set { setThemePicker(self, "setBackgroundColor:", newValue) }
  18. }
  19. var theme_tintColor: ThemeColorPicker? {
  20. get { return getThemePicker(self, "setTintColor:") as? ThemeColorPicker }
  21. set { setThemePicker(self, "setTintColor:", newValue) }
  22. }
  23. }
  24. @objc public extension UIApplication
  25. {
  26. #if os(iOS)
  27. func theme_setStatusBarStyle(_ picker: ThemeStatusBarStylePicker, animated: Bool) {
  28. picker.animated = animated
  29. setThemePicker(self, "setStatusBarStyle:animated:", picker)
  30. }
  31. #endif
  32. }
  33. @objc public extension UIBarItem
  34. {
  35. func theme_setTitleTextAttributes(_ picker: ThemeDictionaryPicker?, forState state: UIControl.State) {
  36. let statePicker = makeStatePicker(self, "setTitleTextAttributes:forState:", picker, state)
  37. setThemePicker(self, "setTitleTextAttributes:forState:", statePicker)
  38. }
  39. }
  40. @objc public extension UIBarButtonItem
  41. {
  42. var theme_tintColor: ThemeColorPicker? {
  43. get { return getThemePicker(self, "setTintColor:") as? ThemeColorPicker }
  44. set { setThemePicker(self, "setTintColor:", newValue) }
  45. }
  46. }
  47. @objc public extension UILabel
  48. {
  49. var theme_font: ThemeFontPicker? {
  50. get { return getThemePicker(self, "setFont:") as? ThemeFontPicker }
  51. set { setThemePicker(self, "setFont:", newValue) }
  52. }
  53. var theme_textColor: ThemeColorPicker? {
  54. get { return getThemePicker(self, "setTextColor:") as? ThemeColorPicker }
  55. set { setThemePicker(self, "setTextColor:", newValue) }
  56. }
  57. var theme_highlightedTextColor: ThemeColorPicker? {
  58. get { return getThemePicker(self, "setHighlightedTextColor:") as? ThemeColorPicker }
  59. set { setThemePicker(self, "setHighlightedTextColor:", newValue) }
  60. }
  61. var theme_shadowColor: ThemeColorPicker? {
  62. get { return getThemePicker(self, "setShadowColor:") as? ThemeColorPicker }
  63. set { setThemePicker(self, "setShadowColor:", newValue) }
  64. }
  65. }
  66. @objc public extension UINavigationBar
  67. {
  68. #if os(iOS)
  69. var theme_barStyle: ThemeBarStylePicker? {
  70. get { return getThemePicker(self, "setBarStyle:") as? ThemeBarStylePicker }
  71. set { setThemePicker(self, "setBarStyle:", newValue) }
  72. }
  73. #endif
  74. var theme_barTintColor: ThemeColorPicker? {
  75. get { return getThemePicker(self, "setBarTintColor:") as? ThemeColorPicker }
  76. set { setThemePicker(self, "setBarTintColor:", newValue) }
  77. }
  78. var theme_titleTextAttributes: ThemeDictionaryPicker? {
  79. get { return getThemePicker(self, "setTitleTextAttributes:") as? ThemeDictionaryPicker }
  80. set { setThemePicker(self, "setTitleTextAttributes:", newValue) }
  81. }
  82. var theme_largeTitleTextAttributes: ThemeDictionaryPicker? {
  83. get { return getThemePicker(self, "setLargeTitleTextAttributes:") as? ThemeDictionaryPicker }
  84. set { setThemePicker(self, "setLargeTitleTextAttributes:", newValue) }
  85. }
  86. }
  87. @objc public extension UITabBar
  88. {
  89. #if os(iOS)
  90. var theme_barStyle: ThemeBarStylePicker? {
  91. get { return getThemePicker(self, "setBarStyle:") as? ThemeBarStylePicker }
  92. set { setThemePicker(self, "setBarStyle:", newValue) }
  93. }
  94. #endif
  95. var theme_barTintColor: ThemeColorPicker? {
  96. get { return getThemePicker(self, "setBarTintColor:") as? ThemeColorPicker }
  97. set { setThemePicker(self, "setBarTintColor:", newValue) }
  98. }
  99. }
  100. @objc public extension UITableView
  101. {
  102. var theme_separatorColor: ThemeColorPicker? {
  103. get { return getThemePicker(self, "setSeparatorColor:") as? ThemeColorPicker }
  104. set { setThemePicker(self, "setSeparatorColor:", newValue) }
  105. }
  106. var theme_sectionIndexColor: ThemeColorPicker? {
  107. get { return getThemePicker(self, "setSectionIndexColor:") as? ThemeColorPicker }
  108. set { setThemePicker(self, "setSectionIndexColor:", newValue) }
  109. }
  110. var theme_sectionIndexBackgroundColor: ThemeColorPicker? {
  111. get { return getThemePicker(self, "setSectionIndexBackgroundColor:") as? ThemeColorPicker }
  112. set { setThemePicker(self, "setSectionIndexBackgroundColor:", newValue) }
  113. }
  114. }
  115. @objc public extension UITextField
  116. {
  117. var theme_font: ThemeFontPicker? {
  118. get { return getThemePicker(self, "setFont:") as? ThemeFontPicker }
  119. set { setThemePicker(self, "setFont:", newValue) }
  120. }
  121. var theme_keyboardAppearance: ThemeKeyboardAppearancePicker? {
  122. get { return getThemePicker(self, "setKeyboardAppearance:") as? ThemeKeyboardAppearancePicker }
  123. set { setThemePicker(self, "setKeyboardAppearance:", newValue) }
  124. }
  125. var theme_textColor: ThemeColorPicker? {
  126. get { return getThemePicker(self, "setTextColor:") as? ThemeColorPicker }
  127. set { setThemePicker(self, "setTextColor:", newValue) }
  128. }
  129. var theme_placeholderAttributes: ThemeDictionaryPicker? {
  130. get { return getThemePicker(self, "updatePlaceholderAttributes:") as? ThemeDictionaryPicker }
  131. set { setThemePicker(self, "updatePlaceholderAttributes:", newValue) }
  132. }
  133. }
  134. @objc public extension UITextView
  135. {
  136. var theme_font: ThemeFontPicker? {
  137. get { return getThemePicker(self, "setFont:") as? ThemeFontPicker }
  138. set { setThemePicker(self, "setFont:", newValue) }
  139. }
  140. var theme_keyboardAppearance: ThemeKeyboardAppearancePicker? {
  141. get { return getThemePicker(self, "setKeyboardAppearance:") as? ThemeKeyboardAppearancePicker }
  142. set { setThemePicker(self, "setKeyboardAppearance:", newValue) }
  143. }
  144. var theme_textColor: ThemeColorPicker? {
  145. get { return getThemePicker(self, "setTextColor:") as? ThemeColorPicker }
  146. set { setThemePicker(self, "setTextColor:", newValue) }
  147. }
  148. }
  149. @objc public extension UISearchBar
  150. {
  151. #if os(iOS)
  152. var theme_barStyle: ThemeBarStylePicker? {
  153. get { return getThemePicker(self, "setBarStyle:") as? ThemeBarStylePicker }
  154. set { setThemePicker(self, "setBarStyle:", newValue) }
  155. }
  156. #endif
  157. var theme_keyboardAppearance: ThemeKeyboardAppearancePicker? {
  158. get { return getThemePicker(self, "setKeyboardAppearance:") as? ThemeKeyboardAppearancePicker }
  159. set { setThemePicker(self, "setKeyboardAppearance:", newValue) }
  160. }
  161. var theme_barTintColor: ThemeColorPicker? {
  162. get { return getThemePicker(self, "setBarTintColor:") as? ThemeColorPicker }
  163. set { setThemePicker(self, "setBarTintColor:", newValue) }
  164. }
  165. }
  166. @objc public extension UIProgressView
  167. {
  168. var theme_progressTintColor: ThemeColorPicker? {
  169. get { return getThemePicker(self, "setProgressTintColor:") as? ThemeColorPicker }
  170. set { setThemePicker(self, "setProgressTintColor:", newValue) }
  171. }
  172. var theme_trackTintColor: ThemeColorPicker? {
  173. get { return getThemePicker(self, "setTrackTintColor:") as? ThemeColorPicker }
  174. set { setThemePicker(self, "setTrackTintColor:", newValue) }
  175. }
  176. }
  177. @objc public extension UIPageControl
  178. {
  179. var theme_pageIndicatorTintColor: ThemeColorPicker? {
  180. get { return getThemePicker(self, "setPageIndicatorTintColor:") as? ThemeColorPicker }
  181. set { setThemePicker(self, "setPageIndicatorTintColor:", newValue) }
  182. }
  183. var theme_currentPageIndicatorTintColor: ThemeColorPicker? {
  184. get { return getThemePicker(self, "setCurrentPageIndicatorTintColor:") as? ThemeColorPicker }
  185. set { setThemePicker(self, "setCurrentPageIndicatorTintColor:", newValue) }
  186. }
  187. }
  188. @objc public extension UIImageView
  189. {
  190. var theme_image: ThemeImagePicker? {
  191. get { return getThemePicker(self, "setImage:") as? ThemeImagePicker }
  192. set { setThemePicker(self, "setImage:", newValue) }
  193. }
  194. }
  195. @objc public extension UIActivityIndicatorView
  196. {
  197. var theme_activityIndicatorViewStyle: ThemeActivityIndicatorViewStylePicker? {
  198. get { return getThemePicker(self, "setActivityIndicatorViewStyle:") as? ThemeActivityIndicatorViewStylePicker }
  199. set { setThemePicker(self, "setActivityIndicatorViewStyle:", newValue) }
  200. }
  201. }
  202. @objc public extension UIScrollView
  203. {
  204. var theme_indicatorStyle: ThemeScrollViewIndicatorStylePicker? {
  205. get { return getThemePicker(self, "setIndicatorStyle:") as? ThemeScrollViewIndicatorStylePicker }
  206. set { setThemePicker(self, "setIndicatorStyle:", newValue) }
  207. }
  208. }
  209. @objc public extension UIButton
  210. {
  211. func theme_setImage(_ picker: ThemeImagePicker?, forState state: UIControl.State) {
  212. let statePicker = makeStatePicker(self, "setImage:forState:", picker, state)
  213. setThemePicker(self, "setImage:forState:", statePicker)
  214. }
  215. func theme_setBackgroundImage(_ picker: ThemeImagePicker?, forState state: UIControl.State) {
  216. let statePicker = makeStatePicker(self, "setBackgroundImage:forState:", picker, state)
  217. setThemePicker(self, "setBackgroundImage:forState:", statePicker)
  218. }
  219. func theme_setTitleColor(_ picker: ThemeColorPicker?, forState state: UIControl.State) {
  220. let statePicker = makeStatePicker(self, "setTitleColor:forState:", picker, state)
  221. setThemePicker(self, "setTitleColor:forState:", statePicker)
  222. }
  223. }
  224. @objc public extension CALayer
  225. {
  226. var theme_backgroundColor: ThemeCGColorPicker? {
  227. get { return getThemePicker(self, "setBackgroundColor:") as? ThemeCGColorPicker}
  228. set { setThemePicker(self, "setBackgroundColor:", newValue) }
  229. }
  230. var theme_borderWidth: ThemeCGFloatPicker? {
  231. get { return getThemePicker(self, "setBorderWidth:") as? ThemeCGFloatPicker }
  232. set { setThemePicker(self, "setBorderWidth:", newValue) }
  233. }
  234. var theme_borderColor: ThemeCGColorPicker? {
  235. get { return getThemePicker(self, "setBorderColor:") as? ThemeCGColorPicker }
  236. set { setThemePicker(self, "setBorderColor:", newValue) }
  237. }
  238. var theme_shadowColor: ThemeCGColorPicker? {
  239. get { return getThemePicker(self, "setShadowColor:") as? ThemeCGColorPicker }
  240. set { setThemePicker(self, "setShadowColor:", newValue) }
  241. }
  242. var theme_strokeColor: ThemeCGColorPicker? {
  243. get { return getThemePicker(self, "setStrokeColor:") as? ThemeCGColorPicker }
  244. set { setThemePicker(self, "setStrokeColor:", newValue) }
  245. }
  246. var theme_fillColor: ThemeCGColorPicker?{
  247. get { return getThemePicker(self, "setFillColor:") as? ThemeCGColorPicker }
  248. set { setThemePicker(self, "setFillColor:", newValue) }
  249. }
  250. }
  251. #if os(iOS)
  252. @objc public extension UIToolbar
  253. {
  254. var theme_barStyle: ThemeBarStylePicker? {
  255. get { return getThemePicker(self, "setBarStyle:") as? ThemeBarStylePicker }
  256. set { setThemePicker(self, "setBarStyle:", newValue) }
  257. }
  258. var theme_barTintColor: ThemeColorPicker? {
  259. get { return getThemePicker(self, "setBarTintColor:") as? ThemeColorPicker }
  260. set { setThemePicker(self, "setBarTintColor:", newValue) }
  261. }
  262. }
  263. @objc public extension UISwitch
  264. {
  265. var theme_onTintColor: ThemeColorPicker? {
  266. get { return getThemePicker(self, "setOnTintColor:") as? ThemeColorPicker }
  267. set { setThemePicker(self, "setOnTintColor:", newValue) }
  268. }
  269. var theme_thumbTintColor: ThemeColorPicker? {
  270. get { return getThemePicker(self, "setThumbTintColor:") as? ThemeColorPicker }
  271. set { setThemePicker(self, "setThumbTintColor:", newValue) }
  272. }
  273. }
  274. @objc public extension UISlider
  275. {
  276. var theme_thumbTintColor: ThemeColorPicker? {
  277. get { return getThemePicker(self, "setThumbTintColor:") as? ThemeColorPicker }
  278. set { setThemePicker(self, "setThumbTintColor:", newValue) }
  279. }
  280. var theme_minimumTrackTintColor: ThemeColorPicker? {
  281. get { return getThemePicker(self, "setMinimumTrackTintColor:") as? ThemeColorPicker }
  282. set { setThemePicker(self, "setMinimumTrackTintColor:", newValue) }
  283. }
  284. var theme_maximumTrackTintColor: ThemeColorPicker? {
  285. get { return getThemePicker(self, "setMaximumTrackTintColor:") as? ThemeColorPicker }
  286. set { setThemePicker(self, "setMaximumTrackTintColor:", newValue) }
  287. }
  288. }
  289. @objc public extension UIPopoverPresentationController
  290. {
  291. var theme_backgroundColor: ThemeColorPicker? {
  292. get { return getThemePicker(self, "setBackgroundColor:") as? ThemeColorPicker }
  293. set { setThemePicker(self, "setBackgroundColor:", newValue) }
  294. }
  295. }
  296. @objc public extension UIRefreshControl
  297. {
  298. var theme_titleAttributes: ThemeDictionaryPicker? {
  299. get { return getThemePicker(self, "updateTitleAttributes:") as? ThemeDictionaryPicker }
  300. set { setThemePicker(self, "updateTitleAttributes:", newValue) }
  301. }
  302. }
  303. #endif
  304. private func getThemePicker(
  305. _ object : NSObject,
  306. _ selector : String
  307. ) -> ThemePicker? {
  308. return object.themePickers[selector]
  309. }
  310. private func setThemePicker(
  311. _ object : NSObject,
  312. _ selector : String,
  313. _ picker : ThemePicker?
  314. ) {
  315. object.themePickers[selector] = picker
  316. object.performThemePicker(selector: selector, picker: picker)
  317. }
  318. private func makeStatePicker(
  319. _ object : NSObject,
  320. _ selector : String,
  321. _ picker : ThemePicker?,
  322. _ state : UIControl.State
  323. ) -> ThemePicker? {
  324. var picker = picker
  325. if let statePicker = object.themePickers[selector] as? ThemeStatePicker {
  326. picker = statePicker.setPicker(picker, forState: state)
  327. } else {
  328. picker = ThemeStatePicker(picker: picker, withState: state)
  329. }
  330. return picker
  331. }