| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- //
- // FDTool.m
- // timePicker
- //
- // Created by xuansa on 2017/1/10.
- // Copyright © 2017年 xuansa. All rights reserved.
- //
- #import "FDTool.h"
- /**
- * 2.返回一个RGBA格式的UIColor对象
- */
- #define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
- /**
- * 3.返回一个RGB格式的UIColor对象
- */
- #define RGB(r, g, b) RGBA(r, g, b, 1.0f)
- /**
- * 4.弱引用
- */
- #define STWeakSelf __weak typeof(self) weakSelf = self;
- @interface FDTool ()
- @property (nonatomic, strong, nullable)UIButton *buttonLeft;
- @property (nonatomic, strong, nullable)UILabel *labelTitle;
- @property (nonatomic, strong, nullable)UIButton *buttonRight;
- @end
- @implementation FDTool
- #pragma mark - --- init 视图初始化 ---
- - (instancetype)initWithTitle:(nullable NSString *)title
- cancelButtonTitle:(nullable NSString *)cancelButtonTitle
- okButtonTitle:(nullable NSString *)okButtonTitle
- addTarget:(nullable id)target
- cancelAction:(SEL)cancelAction
- okAction:(SEL)okAction{
-
- self = [self init];
-
- [self.labelTitle setText:title];
-
- [self.buttonLeft setTitle:cancelButtonTitle forState:UIControlStateNormal];
- [self.buttonLeft addTarget:target action:cancelAction forControlEvents:UIControlEventTouchUpInside];
-
- [self.buttonRight setTitle:okButtonTitle forState:UIControlStateNormal];
- [self.buttonRight addTarget:target action:okAction forControlEvents:UIControlEventTouchUpInside];
-
-
-
- return self;
-
- }
- - (instancetype)init
- {
- if (self = [super init]) {
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
- _title = nil;
- _font = [UIFont systemFontOfSize:15];
- _labelTitleColor = [UIColor blackColor];
- _buttonTitleColor = [UIColor blackColor];
- _buttonBackgroundColor = [UIColor blueColor];
- _borderButtonColor = RGB(205, 205, 205);
-
- self.bounds = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 44);
- [self setBackgroundColor:[UIColor whiteColor]];
- [self addSubview:self.labelTitle];
- [self addSubview:self.buttonLeft];
- [self addSubview:self.buttonRight];
- }
- #pragma mark - --- delegate 视图委托 ---
- #pragma mark - --- event response 事件相应 ---
- #pragma mark - --- private methods 私有方法 ---
- #pragma mark - --- setters 属性 ---
- - (void)setTitle:(NSString *)title
- {
- _title = title;
- [self.labelTitle setText:title];
- }
- - (void)setFont:(UIFont *)font
- {
- _font = font;
- [self.buttonLeft.titleLabel setFont:font];
- [self.buttonRight.titleLabel setFont:font];
- [self.labelTitle setFont:font];
- }
- - (void)setButtonTitleColor:(UIColor *)buttonTitleColor {
- _buttonTitleColor = buttonTitleColor;
- [self.buttonLeft setTitleColor:buttonTitleColor forState:UIControlStateNormal];
- [self.buttonRight setTitleColor:buttonTitleColor forState:UIControlStateNormal];
- }
- - (void)setLabelTitleColor:(UIColor *)labelTitleColor {
- _labelTitleColor = labelTitleColor;
- [self.labelTitle setTextColor:labelTitleColor];
- }
- - (void)setButtonBackgroundColor:(UIColor *)buttonBackgroundColor {
- _buttonBackgroundColor = buttonBackgroundColor;
- [self.buttonLeft setBackgroundColor:buttonBackgroundColor];
- [self.buttonRight setBackgroundColor:buttonBackgroundColor];
- }
- - (void)setBorderButtonColor:(UIColor *)borderButtonColor
- {
- _borderButtonColor = borderButtonColor;
- [self.buttonLeft addBorderColor:borderButtonColor];
- [self.buttonRight addBorderColor:borderButtonColor];
- }
- #pragma mark - --- getters 属性 ---
- - (UIButton *)buttonLeft
- {
- if (!_buttonLeft) {
- CGFloat leftX = 16;
- CGFloat leftY = 5;
- CGFloat leftW = 64;
- CGFloat leftH = 34;
- _buttonLeft = [[UIButton alloc]initWithFrame:CGRectMake(leftX, leftY, leftW, leftH)];
- [_buttonLeft setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
- [_buttonLeft addBorderColor:self.borderButtonColor];
- [_buttonLeft.titleLabel setFont:self.font];
- }
- return _buttonLeft;
- }
- // CGRectGetWidth([UIScreen mainScreen].bounds)
- // CGRectGetHeight([UIScreen mainScreen].bounds)
- - (UIButton *)buttonRight
- {
- if (!_buttonRight) {
- CGFloat rightW = 64;
- CGFloat rightH = 34;
- CGFloat rightX = CGRectGetWidth([UIScreen mainScreen].bounds) - rightW - 16;
- CGFloat rightY = 5;
- _buttonRight = [[UIButton alloc]initWithFrame:CGRectMake(rightX, rightY, rightW, rightH)];
- [_buttonRight setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
- [_buttonRight addBorderColor:self.borderButtonColor];
- [_buttonRight.titleLabel setFont:self.font];
- }
- return _buttonRight;
- }
- - (UILabel *)labelTitle
- {
- if (!_labelTitle) {
- CGFloat titleX = self.buttonLeft.right + 5;
- CGFloat titleY = 0;
- CGFloat titleW = CGRectGetWidth([UIScreen mainScreen].bounds) - titleX * 2;
- CGFloat titleH = 44;
- _labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(titleX, titleY, titleW, titleH)];
- [_labelTitle setTextAlignment:NSTextAlignmentCenter];
- [_labelTitle setTextColor:self.labelTitleColor];
- [_labelTitle setFont:self.font];
- _labelTitle.adjustsFontSizeToFitWidth = YES;
- }
- return _labelTitle;
- }
- @end
- @implementation UIView (FD)
- - (void)setX:(CGFloat)x
- {
- CGRect frame = self.frame;
- frame.origin.x = x;
- self.frame = frame;
- }
- - (void)setY:(CGFloat)y
- {
- CGRect frame = self.frame;
- frame.origin.y = y;
- self.frame = frame;
- }
- - (CGFloat)x
- {
- return self.frame.origin.x;
- }
- - (CGFloat)y
- {
- return self.frame.origin.y;
- }
- - (void)setWidth:(CGFloat)width
- {
- CGRect frame = self.frame;
- frame.size.width = width;
- self.frame = frame;
- }
- - (void)setHeight:(CGFloat)height
- {
- CGRect frame = self.frame;
- frame.size.height = height;
- self.frame = frame;
- }
- - (CGFloat)height
- {
- return self.frame.size.height;
- }
- - (CGFloat)width
- {
- return self.frame.size.width;
- }
- - (UIView * (^)(CGFloat x))setX
- {
- return ^(CGFloat x) {
- self.x = x;
- return self;
- };
- }
- - (void)setCenterX:(CGFloat)centerX
- {
- CGPoint center = self.center;
- center.x = centerX;
- self.center = center;
- }
- - (CGFloat)centerX
- {
- return self.center.x;
- }
- - (void)setCenterY:(CGFloat)centerY
- {
- CGPoint center = self.center;
- center.y = centerY;
- self.center = center;
- }
- - (CGFloat)centerY
- {
- return self.center.y;
- }
- - (void)setSize:(CGSize)size
- {
- CGRect frame = self.frame;
- frame.size = size;
- self.frame = frame;
- }
- - (CGSize)size
- {
- return self.frame.size;
- }
- - (void)setOrigin:(CGPoint)origin
- {
- CGRect frame = self.frame;
- frame.origin = origin;
- self.frame = frame;
- }
- - (CGPoint)origin
- {
- return self.frame.origin;
- }
- - (CGFloat)left {
- return self.frame.origin.x;
- }
- - (void)setLeft:(CGFloat)x {
- CGRect frame = self.frame;
- frame.origin.x = x;
- self.frame = frame;
- }
- - (CGFloat)top {
- return self.frame.origin.y;
- }
- - (void)setTop:(CGFloat)y {
- CGRect frame = self.frame;
- frame.origin.y = y;
- self.frame = frame;
- }
- - (CGFloat)right {
- return self.frame.origin.x + self.frame.size.width;
- }
- - (void)setRight:(CGFloat)right {
- CGRect frame = self.frame;
- frame.origin.x = right - frame.size.width;
- self.frame = frame;
- }
- - (CGFloat)bottom {
- return self.frame.origin.y + self.frame.size.height;
- }
- - (void)setBottom:(CGFloat)bottom {
- CGRect frame = self.frame;
- frame.origin.y = bottom - frame.size.height;
- self.frame = frame;
- }
- - (UIView *(^)(UIColor *color)) setColor
- {
- return ^ (UIColor *color) {
- self.backgroundColor = color;
- return self;
- };
- }
- - (UIView *(^)(CGRect frame)) setFrame
- {
- return ^ (CGRect frame) {
- self.frame = frame;
- return self;
- };
- }
- - (UIView *(^)(CGSize size)) setSize
- {
- return ^ (CGSize size) {
- self.bounds = CGRectMake(0, 0, size.width, size.height);
- return self;
- };
- }
- - (UIView *(^)(CGPoint point)) setCenter
- {
- return ^ (CGPoint point) {
- self.center = point;
- return self;
- };
- }
- - (UIView *(^)(NSInteger tag)) setTag
- {
- return ^ (NSInteger tag) {
- self.tag = tag;
- return self;
- };
- }
- - (void)addTarget:(id)target
- action:(SEL)action;
- {
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
- action:action];
- self.userInteractionEnabled = YES;
- [self addGestureRecognizer:tap];
- }
- - (void)addBorderColor:(UIColor *)color{
- [self.layer setBorderColor:color.CGColor];
- [self.layer setBorderWidth:0.5];
- [self.layer setCornerRadius:4];
- }
- @end
|