UIButton+RQExtension.m 4.7 KB

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