FDTool.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // FDTool.m
  3. // timePicker
  4. //
  5. // Created by xuansa on 2017/1/10.
  6. // Copyright © 2017年 xuansa. All rights reserved.
  7. //
  8. #import "FDTool.h"
  9. /**
  10. * 2.返回一个RGBA格式的UIColor对象
  11. */
  12. #define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  13. /**
  14. * 3.返回一个RGB格式的UIColor对象
  15. */
  16. #define RGB(r, g, b) RGBA(r, g, b, 1.0f)
  17. /**
  18. * 4.弱引用
  19. */
  20. #define STWeakSelf __weak typeof(self) weakSelf = self;
  21. @interface FDTool ()
  22. @property (nonatomic, strong, nullable)UIButton *buttonLeft;
  23. @property (nonatomic, strong, nullable)UILabel *labelTitle;
  24. @property (nonatomic, strong, nullable)UIButton *buttonRight;
  25. @end
  26. @implementation FDTool
  27. #pragma mark - --- init 视图初始化 ---
  28. - (instancetype)initWithTitle:(nullable NSString *)title
  29. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  30. okButtonTitle:(nullable NSString *)okButtonTitle
  31. addTarget:(nullable id)target
  32. cancelAction:(SEL)cancelAction
  33. okAction:(SEL)okAction{
  34. self = [self init];
  35. [self.labelTitle setText:title];
  36. [self.buttonLeft setTitle:cancelButtonTitle forState:UIControlStateNormal];
  37. [self.buttonLeft addTarget:target action:cancelAction forControlEvents:UIControlEventTouchUpInside];
  38. [self.buttonRight setTitle:okButtonTitle forState:UIControlStateNormal];
  39. [self.buttonRight addTarget:target action:okAction forControlEvents:UIControlEventTouchUpInside];
  40. return self;
  41. }
  42. - (instancetype)init
  43. {
  44. if (self = [super init]) {
  45. [self setupUI];
  46. }
  47. return self;
  48. }
  49. - (void)setupUI {
  50. _title = nil;
  51. _font = [UIFont systemFontOfSize:15];
  52. _labelTitleColor = [UIColor blackColor];
  53. _buttonTitleColor = [UIColor blackColor];
  54. _buttonBackgroundColor = [UIColor blueColor];
  55. _borderButtonColor = RGB(205, 205, 205);
  56. self.bounds = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 44);
  57. [self setBackgroundColor:[UIColor whiteColor]];
  58. [self addSubview:self.labelTitle];
  59. [self addSubview:self.buttonLeft];
  60. [self addSubview:self.buttonRight];
  61. }
  62. #pragma mark - --- delegate 视图委托 ---
  63. #pragma mark - --- event response 事件相应 ---
  64. #pragma mark - --- private methods 私有方法 ---
  65. #pragma mark - --- setters 属性 ---
  66. - (void)setTitle:(NSString *)title
  67. {
  68. _title = title;
  69. [self.labelTitle setText:title];
  70. }
  71. - (void)setFont:(UIFont *)font
  72. {
  73. _font = font;
  74. [self.buttonLeft.titleLabel setFont:font];
  75. [self.buttonRight.titleLabel setFont:font];
  76. [self.labelTitle setFont:font];
  77. }
  78. - (void)setButtonTitleColor:(UIColor *)buttonTitleColor {
  79. _buttonTitleColor = buttonTitleColor;
  80. [self.buttonLeft setTitleColor:buttonTitleColor forState:UIControlStateNormal];
  81. [self.buttonRight setTitleColor:buttonTitleColor forState:UIControlStateNormal];
  82. }
  83. - (void)setLabelTitleColor:(UIColor *)labelTitleColor {
  84. _labelTitleColor = labelTitleColor;
  85. [self.labelTitle setTextColor:labelTitleColor];
  86. }
  87. - (void)setButtonBackgroundColor:(UIColor *)buttonBackgroundColor {
  88. _buttonBackgroundColor = buttonBackgroundColor;
  89. [self.buttonLeft setBackgroundColor:buttonBackgroundColor];
  90. [self.buttonRight setBackgroundColor:buttonBackgroundColor];
  91. }
  92. - (void)setBorderButtonColor:(UIColor *)borderButtonColor
  93. {
  94. _borderButtonColor = borderButtonColor;
  95. [self.buttonLeft addBorderColor:borderButtonColor];
  96. [self.buttonRight addBorderColor:borderButtonColor];
  97. }
  98. #pragma mark - --- getters 属性 ---
  99. - (UIButton *)buttonLeft
  100. {
  101. if (!_buttonLeft) {
  102. CGFloat leftX = 16;
  103. CGFloat leftY = 5;
  104. CGFloat leftW = 64;
  105. CGFloat leftH = 34;
  106. _buttonLeft = [[UIButton alloc]initWithFrame:CGRectMake(leftX, leftY, leftW, leftH)];
  107. [_buttonLeft setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
  108. [_buttonLeft addBorderColor:self.borderButtonColor];
  109. [_buttonLeft.titleLabel setFont:self.font];
  110. }
  111. return _buttonLeft;
  112. }
  113. // CGRectGetWidth([UIScreen mainScreen].bounds)
  114. // CGRectGetHeight([UIScreen mainScreen].bounds)
  115. - (UIButton *)buttonRight
  116. {
  117. if (!_buttonRight) {
  118. CGFloat rightW = 64;
  119. CGFloat rightH = 34;
  120. CGFloat rightX = CGRectGetWidth([UIScreen mainScreen].bounds) - rightW - 16;
  121. CGFloat rightY = 5;
  122. _buttonRight = [[UIButton alloc]initWithFrame:CGRectMake(rightX, rightY, rightW, rightH)];
  123. [_buttonRight setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
  124. [_buttonRight addBorderColor:self.borderButtonColor];
  125. [_buttonRight.titleLabel setFont:self.font];
  126. }
  127. return _buttonRight;
  128. }
  129. - (UILabel *)labelTitle
  130. {
  131. if (!_labelTitle) {
  132. CGFloat titleX = self.buttonLeft.right + 5;
  133. CGFloat titleY = 0;
  134. CGFloat titleW = CGRectGetWidth([UIScreen mainScreen].bounds) - titleX * 2;
  135. CGFloat titleH = 44;
  136. _labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(titleX, titleY, titleW, titleH)];
  137. [_labelTitle setTextAlignment:NSTextAlignmentCenter];
  138. [_labelTitle setTextColor:self.labelTitleColor];
  139. [_labelTitle setFont:self.font];
  140. _labelTitle.adjustsFontSizeToFitWidth = YES;
  141. }
  142. return _labelTitle;
  143. }
  144. @end
  145. @implementation UIView (FD)
  146. - (void)setX:(CGFloat)x
  147. {
  148. CGRect frame = self.frame;
  149. frame.origin.x = x;
  150. self.frame = frame;
  151. }
  152. - (void)setY:(CGFloat)y
  153. {
  154. CGRect frame = self.frame;
  155. frame.origin.y = y;
  156. self.frame = frame;
  157. }
  158. - (CGFloat)x
  159. {
  160. return self.frame.origin.x;
  161. }
  162. - (CGFloat)y
  163. {
  164. return self.frame.origin.y;
  165. }
  166. - (void)setWidth:(CGFloat)width
  167. {
  168. CGRect frame = self.frame;
  169. frame.size.width = width;
  170. self.frame = frame;
  171. }
  172. - (void)setHeight:(CGFloat)height
  173. {
  174. CGRect frame = self.frame;
  175. frame.size.height = height;
  176. self.frame = frame;
  177. }
  178. - (CGFloat)height
  179. {
  180. return self.frame.size.height;
  181. }
  182. - (CGFloat)width
  183. {
  184. return self.frame.size.width;
  185. }
  186. - (UIView * (^)(CGFloat x))setX
  187. {
  188. return ^(CGFloat x) {
  189. self.x = x;
  190. return self;
  191. };
  192. }
  193. - (void)setCenterX:(CGFloat)centerX
  194. {
  195. CGPoint center = self.center;
  196. center.x = centerX;
  197. self.center = center;
  198. }
  199. - (CGFloat)centerX
  200. {
  201. return self.center.x;
  202. }
  203. - (void)setCenterY:(CGFloat)centerY
  204. {
  205. CGPoint center = self.center;
  206. center.y = centerY;
  207. self.center = center;
  208. }
  209. - (CGFloat)centerY
  210. {
  211. return self.center.y;
  212. }
  213. - (void)setSize:(CGSize)size
  214. {
  215. CGRect frame = self.frame;
  216. frame.size = size;
  217. self.frame = frame;
  218. }
  219. - (CGSize)size
  220. {
  221. return self.frame.size;
  222. }
  223. - (void)setOrigin:(CGPoint)origin
  224. {
  225. CGRect frame = self.frame;
  226. frame.origin = origin;
  227. self.frame = frame;
  228. }
  229. - (CGPoint)origin
  230. {
  231. return self.frame.origin;
  232. }
  233. - (CGFloat)left {
  234. return self.frame.origin.x;
  235. }
  236. - (void)setLeft:(CGFloat)x {
  237. CGRect frame = self.frame;
  238. frame.origin.x = x;
  239. self.frame = frame;
  240. }
  241. - (CGFloat)top {
  242. return self.frame.origin.y;
  243. }
  244. - (void)setTop:(CGFloat)y {
  245. CGRect frame = self.frame;
  246. frame.origin.y = y;
  247. self.frame = frame;
  248. }
  249. - (CGFloat)right {
  250. return self.frame.origin.x + self.frame.size.width;
  251. }
  252. - (void)setRight:(CGFloat)right {
  253. CGRect frame = self.frame;
  254. frame.origin.x = right - frame.size.width;
  255. self.frame = frame;
  256. }
  257. - (CGFloat)bottom {
  258. return self.frame.origin.y + self.frame.size.height;
  259. }
  260. - (void)setBottom:(CGFloat)bottom {
  261. CGRect frame = self.frame;
  262. frame.origin.y = bottom - frame.size.height;
  263. self.frame = frame;
  264. }
  265. - (UIView *(^)(UIColor *color)) setColor
  266. {
  267. return ^ (UIColor *color) {
  268. self.backgroundColor = color;
  269. return self;
  270. };
  271. }
  272. - (UIView *(^)(CGRect frame)) setFrame
  273. {
  274. return ^ (CGRect frame) {
  275. self.frame = frame;
  276. return self;
  277. };
  278. }
  279. - (UIView *(^)(CGSize size)) setSize
  280. {
  281. return ^ (CGSize size) {
  282. self.bounds = CGRectMake(0, 0, size.width, size.height);
  283. return self;
  284. };
  285. }
  286. - (UIView *(^)(CGPoint point)) setCenter
  287. {
  288. return ^ (CGPoint point) {
  289. self.center = point;
  290. return self;
  291. };
  292. }
  293. - (UIView *(^)(NSInteger tag)) setTag
  294. {
  295. return ^ (NSInteger tag) {
  296. self.tag = tag;
  297. return self;
  298. };
  299. }
  300. - (void)addTarget:(id)target
  301. action:(SEL)action;
  302. {
  303. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
  304. action:action];
  305. self.userInteractionEnabled = YES;
  306. [self addGestureRecognizer:tap];
  307. }
  308. - (void)addBorderColor:(UIColor *)color{
  309. [self.layer setBorderColor:color.CGColor];
  310. [self.layer setBorderWidth:0.5];
  311. [self.layer setCornerRadius:4];
  312. }
  313. @end