// // UIButton+KXExtension.m // CAISHEN // // Created by jikaipeng on 2018/9/4. // Copyright © 2018年 kuxuan. All rights reserved. // #import "UIButton+KXExtension.h" @implementation UIButton (KXExtension) static char actionBlockKey; static char DHButtonTypeKey; /* 快速调用按钮的触摸事件 */ - (void)addActionBlockWith:(UIControlEvents)controlEvent withBlock:(ButtonActionBlock)block { objc_setAssociatedObject(self, &actionBlockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC); [self addTarget:self action:@selector(callActionBlock:) forControlEvents:controlEvent]; } - (void)callActionBlock:(id)sender { ButtonActionBlock block = (ButtonActionBlock)objc_getAssociatedObject(self, &actionBlockKey); if (block) { block(); } } -(void)setButtonType:(KXButtonType)buttonType { objc_setAssociatedObject(self, &DHButtonTypeKey, @(buttonType), OBJC_ASSOCIATION_ASSIGN); } -(KXButtonType)buttonType { NSNumber *hudBool = objc_getAssociatedObject(self, &DHButtonTypeKey); return [hudBool integerValue]; } + (instancetype)buttonWithDHButtonType:(KXButtonType)type { UIButton *btn = [self buttonWithType:UIButtonTypeCustom]; btn.buttonType = type; switch (btn.buttonType) { case KXButtonTypeRedButton: { btn.layer.cornerRadius = 5.0f; } break; default: break; } return btn; } /** 快速创建一个按钮 */ + (UIButton *)createBtnWithTitle:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target selector:(SEL)action{ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; NSAttributedString *normalAttibute = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font}]; [button setAttributedTitle:normalAttibute forState:UIControlStateNormal]; button.showsTouchWhenHighlighted = NO; [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; return button; } @end