No Description

UIButton+KXExtension.m 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // UIButton+KXExtension.m
  3. // CAISHEN
  4. //
  5. // Created by jikaipeng on 2018/9/4.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "UIButton+KXExtension.h"
  9. @implementation UIButton (KXExtension)
  10. static char actionBlockKey;
  11. static char DHButtonTypeKey;
  12. /*
  13. 快速调用按钮的触摸事件
  14. */
  15. - (void)addActionBlockWith:(UIControlEvents)controlEvent withBlock:(ButtonActionBlock)block
  16. {
  17. objc_setAssociatedObject(self, &actionBlockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
  18. [self addTarget:self action:@selector(callActionBlock:) forControlEvents:controlEvent];
  19. }
  20. - (void)callActionBlock:(id)sender
  21. {
  22. ButtonActionBlock block = (ButtonActionBlock)objc_getAssociatedObject(self, &actionBlockKey);
  23. if (block)
  24. {
  25. block();
  26. }
  27. }
  28. -(void)setButtonType:(KXButtonType)buttonType {
  29. objc_setAssociatedObject(self, &DHButtonTypeKey, @(buttonType), OBJC_ASSOCIATION_ASSIGN);
  30. }
  31. -(KXButtonType)buttonType {
  32. NSNumber *hudBool = objc_getAssociatedObject(self, &DHButtonTypeKey);
  33. return [hudBool integerValue];
  34. }
  35. + (instancetype)buttonWithDHButtonType:(KXButtonType)type
  36. {
  37. UIButton *btn = [self buttonWithType:UIButtonTypeCustom];
  38. btn.buttonType = type;
  39. switch (btn.buttonType)
  40. {
  41. case KXButtonTypeRedButton:
  42. {
  43. btn.layer.cornerRadius = 5.0f;
  44. }
  45. break;
  46. default:
  47. break;
  48. }
  49. return btn;
  50. }
  51. /**
  52. 快速创建一个按钮
  53. */
  54. + (UIButton *)createBtnWithTitle:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target selector:(SEL)action{
  55. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  56. NSAttributedString *normalAttibute = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font}];
  57. [button setAttributedTitle:normalAttibute forState:UIControlStateNormal];
  58. button.showsTouchWhenHighlighted = NO;
  59. [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  60. return button;
  61. }
  62. @end