UIButton+RQExtension.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // UIButton+RQExtension.m
  3. // XinShouJiaDao
  4. //
  5. // Created by 张嵘 on 2021/7/7.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "UIButton+RQExtension.h"
  9. #import <objc/runtime.h>
  10. static const void *UIButtonBlockKey = &UIButtonBlockKey;
  11. @implementation UIButton (RQExtension)
  12. - (void)setFixWidthScreenFont:(float)fixWidthScreenFont {
  13. if (fixWidthScreenFont > 0 ) {
  14. self.titleLabel.font = [UIFont systemFontOfSize:RQ_WIDTH(fixWidthScreenFont)];
  15. } else {
  16. self.titleLabel.font = self.titleLabel.font;
  17. }
  18. }
  19. - (float)fixWidthScreenFont {
  20. return self.fixWidthScreenFont;
  21. }
  22. - (void)setRq_titleStr:(NSString *)rq_titleStr {
  23. [self setTitleNormal:rq_titleStr];
  24. }
  25. - (NSString *)rq_titleStr {
  26. return self.titleLabel.text;
  27. }
  28. - (void)addActionHandler:(TouchedBlock)touchHandler {
  29. objc_setAssociatedObject(self, UIButtonBlockKey, touchHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
  30. [self addTarget:self action:@selector(actionTouched:) forControlEvents:UIControlEventTouchUpInside];
  31. }
  32. - (void)actionTouched:(UIButton *)btn {
  33. TouchedBlock block = objc_getAssociatedObject(self, UIButtonBlockKey);
  34. if (block) {
  35. block(btn.tag);
  36. }
  37. }
  38. - (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
  39. [self setImage:image withTitle:title Font:10 forState:stateType];
  40. }
  41. - (void) setImage:(UIImage *)image withTitle:(NSString *)title Font:(CGFloat)font forState:(UIControlState)stateType{
  42. [self.titleLabel setContentMode:UIViewContentModeCenter];
  43. [self.titleLabel setBackgroundColor:[UIColor clearColor]];
  44. [self setTitleColor:RQ_MAIN_TEXT_COLOR_1 forState:stateType];
  45. // [self setTitleColor:kTitleColor forState:stateType];
  46. [self setTitle:title forState:stateType];
  47. [self.titleLabel setFont:RQRegularFont(font)];
  48. [self setImage:image forState:stateType];
  49. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  50. }
  51. - (void)setTitle:(NSString*)title textColor:(UIColor*)color Font:(CGFloat)font fotState:(UIControlState)stateType {
  52. [self setTitle:title forState:stateType];
  53. [self setTitleColor:color forState:stateType];
  54. [self.titleLabel setFont:RQRegularFont(font)];
  55. }
  56. - (void)setImage:(UIImage *)image withTitle:(NSString*)title textColor:(UIColor*)color Font:(CGFloat)font fotState:(UIControlState)stateType {
  57. [self.titleLabel setContentMode:UIViewContentModeCenter];
  58. [self.titleLabel setBackgroundColor:[UIColor clearColor]];
  59. [self setTitleColor:color forState:stateType];
  60. [self setTitle:title forState:stateType];
  61. [self.titleLabel setFont:RQRegularFont(font)];
  62. [self setImage:image forState:stateType];
  63. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  64. }
  65. -(void)setTitleNormal:(NSString*)title
  66. {
  67. [self setTitle:title forState:UIControlStateNormal];
  68. }
  69. -(void)setTitleSelect:(NSString*)title
  70. {
  71. [self setTitle:title forState:UIControlStateSelected];
  72. }
  73. -(void)target:(id)obj
  74. {
  75. [self addTarget:obj action:NSSelectorFromString(@"btnClick:") forControlEvents:UIControlEventTouchUpInside];
  76. }
  77. -(void)target:(id)obj tag:(NSInteger)tag
  78. {
  79. self.tag = tag;
  80. [self target:obj];
  81. }
  82. - (void)layoutButtonWithEdgeInsetsStyle:(RQButtonEdgeInsetsStyle)style
  83. imageTitleSpace:(CGFloat)space {
  84. /**
  85. * 知识点:titleEdgeInsets是title相对于其上下左右的inset,跟tableView的contentInset是类似的,
  86. * 如果只有title,那它上下左右都是相对于button的,image也是一样;
  87. * 如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
  88. */
  89. // 1. 得到imageView和titleLabel的宽、高
  90. CGFloat imageWith = self.imageView.image.size.width;
  91. CGFloat imageHeight = self.imageView.image.size.height;
  92. CGFloat labelWidth = 0.0;
  93. CGFloat labelHeight = 0.0;
  94. if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
  95. // 由于iOS8中titleLabel的size为0,用下面的这种设置
  96. labelWidth = self.titleLabel.intrinsicContentSize.width;
  97. labelHeight = self.titleLabel.intrinsicContentSize.height;
  98. } else {
  99. labelWidth = self.titleLabel.frame.size.width;
  100. labelHeight = self.titleLabel.frame.size.height;
  101. }
  102. // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
  103. UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
  104. UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
  105. // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
  106. switch (style) {
  107. case RQButtonEdgeInsetsStyleTop:
  108. {
  109. imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
  110. labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
  111. }
  112. break;
  113. case RQButtonEdgeInsetsStyleLeft:
  114. {
  115. imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
  116. labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
  117. }
  118. break;
  119. case RQButtonEdgeInsetsStyleBottom:
  120. {
  121. imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
  122. labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
  123. }
  124. break;
  125. case RQButtonEdgeInsetsStyleRight:
  126. {
  127. imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
  128. labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
  129. }
  130. break;
  131. default:
  132. break;
  133. }
  134. // 4. 赋值
  135. self.titleEdgeInsets = labelEdgeInsets;
  136. self.imageEdgeInsets = imageEdgeInsets;
  137. }
  138. @end