BRPickerStyle.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // BRPickerStyle.m
  3. // BRPickerViewDemo
  4. //
  5. // Created by renbo on 2019/10/2.
  6. // Copyright © 2019 irenb. All dones reserved.
  7. //
  8. // 最新代码下载地址:https://github.com/91renb/BRPickerView
  9. #import "BRPickerStyle.h"
  10. #import "NSBundle+BRPickerView.h"
  11. // 标题颜色
  12. #define kBRDefaultTextColor BR_RGB_HEX(0x333333, 1.0f)
  13. @implementation BRPickerStyle
  14. /// 设置默认样式
  15. - (UIColor *)maskColor {
  16. if (!_maskColor) {
  17. _maskColor = [UIColor colorWithWhite:0 alpha:0.2f];
  18. }
  19. return _maskColor;
  20. }
  21. - (UIColor *)shadowLineColor {
  22. if (!_shadowLineColor) {
  23. if (@available(iOS 13.0, *)) {
  24. // 边框线颜色,有透明度
  25. _shadowLineColor = [UIColor separatorColor];
  26. } else {
  27. _shadowLineColor = BR_RGB_HEX(0xc6c6c8, 1.0f);
  28. }
  29. }
  30. return _shadowLineColor;
  31. }
  32. - (CGFloat)shadowLineHeight {
  33. if (_shadowLineHeight <= 0 || _shadowLineHeight > 5.0f) {
  34. _shadowLineHeight = 0.5f;
  35. }
  36. return _shadowLineHeight;
  37. }
  38. - (CGFloat)paddingBottom {
  39. if (_paddingBottom <= 0) {
  40. _paddingBottom = BR_BOTTOM_MARGIN;
  41. }
  42. return _paddingBottom;
  43. }
  44. - (UIColor *)titleBarColor {
  45. if (!_titleBarColor) {
  46. if (@available(iOS 13.0, *)) {
  47. // #ffffff(正常)、#1c1c1e(深色)
  48. _titleBarColor = [UIColor secondarySystemGroupedBackgroundColor];
  49. } else {
  50. _titleBarColor = [UIColor whiteColor];
  51. }
  52. }
  53. return _titleBarColor;
  54. }
  55. - (CGFloat)titleBarHeight {
  56. if (!self.hiddenTitleBarView) {
  57. if (_titleBarHeight < 44.0f && (!self.hiddenCancelBtn || !self.hiddenDoneBtn || !self.hiddenTitleLabel)) {
  58. _titleBarHeight = 44.0f;
  59. }
  60. } else {
  61. _titleBarHeight = 0;
  62. }
  63. return _titleBarHeight;
  64. }
  65. - (UIColor *)titleLineColor {
  66. if (!_titleLineColor) {
  67. _titleLineColor = [self br_colorWithLightColor:BR_RGB_HEX(0xededee, 1.0f) darkColor:BR_RGB_HEX(0x18181c, 1.0f)];
  68. }
  69. return _titleLineColor;
  70. }
  71. - (UIColor *)cancelColor {
  72. if (!_cancelColor) {
  73. _cancelColor = [UIColor clearColor];
  74. }
  75. return _cancelColor;
  76. }
  77. - (UIColor *)cancelTextColor {
  78. if (!_cancelTextColor) {
  79. if (@available(iOS 13.0, *)) {
  80. _cancelTextColor = [UIColor labelColor];
  81. } else {
  82. _cancelTextColor = kBRDefaultTextColor;
  83. }
  84. }
  85. return _cancelTextColor;
  86. }
  87. - (UIFont *)cancelTextFont {
  88. if (!_cancelTextFont) {
  89. _cancelTextFont = [UIFont systemFontOfSize:16.0f];
  90. }
  91. return _cancelTextFont;
  92. }
  93. - (NSString *)cancelBtnTitle {
  94. if (!_cancelBtnTitle && !_cancelBtnImage) {
  95. _cancelBtnTitle = [NSBundle br_localizedStringForKey:@"取消" language:self.language];
  96. }
  97. return _cancelBtnTitle;
  98. }
  99. - (CGRect)cancelBtnFrame {
  100. if (CGRectEqualToRect(_cancelBtnFrame, CGRectZero) || _cancelBtnFrame.size.height == 0) {
  101. _cancelBtnFrame = CGRectMake(5, 8, 60, 28);
  102. }
  103. return _cancelBtnFrame;
  104. }
  105. - (UIColor *)titleLabelColor {
  106. if (!_titleLabelColor) {
  107. _titleLabelColor = [UIColor clearColor];
  108. }
  109. return _titleLabelColor;
  110. }
  111. - (UIColor *)titleTextColor {
  112. if (!_titleTextColor) {
  113. if (@available(iOS 13.0, *)) {
  114. _titleTextColor = [UIColor secondaryLabelColor];
  115. } else {
  116. _titleTextColor = BR_RGB_HEX(0x999999, 1.0f);
  117. }
  118. }
  119. return _titleTextColor;
  120. }
  121. - (UIFont *)titleTextFont {
  122. if (!_titleTextFont) {
  123. _titleTextFont = [UIFont systemFontOfSize:15.0f];
  124. }
  125. return _titleTextFont;
  126. }
  127. - (CGRect)titleLabelFrame {
  128. if (CGRectEqualToRect(_titleLabelFrame, CGRectZero) || _titleLabelFrame.size.height == 0) {
  129. _titleLabelFrame = CGRectMake(5 + 60 + 2, 0, BRGetKeyWindow().bounds.size.width - 2 * (5 + 60 + 2), 44);
  130. }
  131. return _titleLabelFrame;
  132. }
  133. - (UIColor *)doneColor {
  134. if (!_doneColor) {
  135. _doneColor = [UIColor clearColor];
  136. }
  137. return _doneColor;
  138. }
  139. - (UIColor *)doneTextColor {
  140. if (!_doneTextColor) {
  141. if (@available(iOS 13.0, *)) {
  142. _doneTextColor = [UIColor labelColor];
  143. } else {
  144. _doneTextColor = kBRDefaultTextColor;
  145. }
  146. }
  147. return _doneTextColor;
  148. }
  149. - (UIFont *)doneTextFont {
  150. if (!_doneTextFont) {
  151. _doneTextFont = [UIFont systemFontOfSize:16.0f];
  152. }
  153. return _doneTextFont;
  154. }
  155. - (NSString *)doneBtnTitle {
  156. if (!_doneBtnTitle && !_doneBtnImage) {
  157. _doneBtnTitle = [NSBundle br_localizedStringForKey:@"确定" language:self.language];
  158. }
  159. return _doneBtnTitle;
  160. }
  161. - (CGRect)doneBtnFrame {
  162. if (CGRectEqualToRect(_doneBtnFrame, CGRectZero) || _doneBtnFrame.size.height == 0) {
  163. _doneBtnFrame = CGRectMake(BRGetKeyWindow().bounds.size.width - 60 - 5, 8, 60, 28);
  164. }
  165. return _doneBtnFrame;
  166. }
  167. - (UIColor *)pickerColor {
  168. if (!_pickerColor) {
  169. if (@available(iOS 13.0, *)) {
  170. // #ffffff(正常)、#1c1c1e(深色)
  171. _pickerColor = [UIColor secondarySystemGroupedBackgroundColor];
  172. } else {
  173. _pickerColor = [UIColor whiteColor];
  174. }
  175. }
  176. return _pickerColor;
  177. }
  178. - (UIColor *)separatorColor {
  179. if (!_separatorColor) {
  180. if (@available(iOS 13.0, *)) {
  181. // 分割线颜色,无透明度
  182. _separatorColor = [UIColor opaqueSeparatorColor];
  183. } else {
  184. _separatorColor = BR_RGB_HEX(0xc6c6c8, 1.0f);
  185. }
  186. }
  187. return _separatorColor;
  188. }
  189. - (UIColor *)pickerTextColor {
  190. if (!_pickerTextColor) {
  191. if (@available(iOS 13.0, *)) {
  192. _pickerTextColor = [UIColor labelColor];
  193. } else {
  194. _pickerTextColor = kBRDefaultTextColor;
  195. }
  196. }
  197. return _pickerTextColor;
  198. }
  199. - (UIFont *)pickerTextFont {
  200. if (!_pickerTextFont) {
  201. _pickerTextFont = [UIFont systemFontOfSize:18.0f];
  202. }
  203. return _pickerTextFont;
  204. }
  205. - (CGFloat)pickerHeight {
  206. if (_pickerHeight < 40) {
  207. _pickerHeight = 216.0f;
  208. }
  209. return _pickerHeight;
  210. }
  211. - (CGFloat)rowHeight {
  212. if (_rowHeight < 20) {
  213. _rowHeight = 35.0f;
  214. }
  215. return _rowHeight;
  216. }
  217. - (NSString *)language {
  218. if (!_language) {
  219. // 跟随系统的首选语言自动改变
  220. // zh-Hans-CN(简体中文)、zh-Hant-CN(繁体中文)、en-CN(美式英语)、en-GB(英式英语)
  221. // 其中`CN`是iOS9以后新增的地区代码,如:CN 代表中国,US 代表美国
  222. _language = [NSLocale preferredLanguages].firstObject;
  223. }
  224. return _language;
  225. }
  226. - (UIColor *)dateUnitTextColor {
  227. if (!_dateUnitTextColor) {
  228. if (@available(iOS 13.0, *)) {
  229. _dateUnitTextColor = [UIColor labelColor];
  230. } else {
  231. _dateUnitTextColor = kBRDefaultTextColor;
  232. }
  233. }
  234. return _dateUnitTextColor;
  235. }
  236. - (UIFont *)dateUnitTextFont {
  237. if (!_dateUnitTextFont) {
  238. _dateUnitTextFont = [UIFont systemFontOfSize:18.0f];
  239. }
  240. return _dateUnitTextFont;
  241. }
  242. #pragma mark - 创建自定义动态颜色(适配深色模式)
  243. - (UIColor *)br_colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
  244. if (@available(iOS 13.0, *)) {
  245. UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
  246. if ([traitCollection userInterfaceStyle] == UIUserInterfaceStyleLight) {
  247. return lightColor;
  248. } else {
  249. return darkColor;
  250. }
  251. }];
  252. return dyColor;
  253. } else {
  254. return lightColor;
  255. }
  256. }
  257. #pragma mark - 弹框模板样式1 - 取消/确定按钮圆角样式
  258. + (instancetype)pickerStyleWithThemeColor:(UIColor *)themeColor {
  259. BRPickerStyle *customStyle = [[self alloc]init];
  260. if (themeColor) {
  261. customStyle.cancelTextColor = themeColor;
  262. customStyle.cancelBorderStyle = BRBorderStyleSolid;
  263. customStyle.doneColor = themeColor;
  264. customStyle.doneTextColor = [UIColor whiteColor];
  265. customStyle.doneBorderStyle = BRBorderStyleFill;
  266. }
  267. return customStyle;
  268. }
  269. #pragma mark - 弹框模板样式2 - 顶部圆角样式 + 完成按钮
  270. + (instancetype)pickerStyleWithDoneTextColor:(UIColor *)doneTextColor {
  271. BRPickerStyle *customStyle = [[self alloc]init];
  272. if (doneTextColor) {
  273. customStyle.topCornerRadius = 16.0f;
  274. customStyle.hiddenCancelBtn = YES;
  275. customStyle.hiddenTitleLine = YES;
  276. customStyle.titleLabelFrame = CGRectMake(20, 4, 100, 40);
  277. customStyle.doneTextColor = doneTextColor;
  278. customStyle.doneTextFont = [UIFont boldSystemFontOfSize:16.0f];
  279. customStyle.doneBtnFrame = CGRectMake(BRGetKeyWindow().bounds.size.width - 60, 4, 60, 40);
  280. customStyle.doneBtnTitle = [NSBundle br_localizedStringForKey:@"完成" language:customStyle.language];
  281. }
  282. return customStyle;
  283. }
  284. #pragma mark - 弹框模板样式3 - 顶部圆角样式 + 图标按钮
  285. + (instancetype)pickerStyleWithDoneBtnImage:(UIImage *)doneBtnImage {
  286. BRPickerStyle *customStyle = [[self alloc]init];
  287. if (doneBtnImage) {
  288. customStyle.topCornerRadius = 16.0f;
  289. customStyle.hiddenTitleLine = YES;
  290. customStyle.hiddenCancelBtn = YES;
  291. customStyle.titleLabelFrame = CGRectMake(20, 4, 100, 40);
  292. customStyle.doneBtnImage = doneBtnImage;
  293. customStyle.doneBtnFrame = CGRectMake(BRGetKeyWindow().bounds.size.width - 44, 4, 40, 40);
  294. }
  295. return customStyle;
  296. }
  297. @end