UIButton+RQExtension.m 5.6 KB

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